Created
April 18, 2021 20:27
-
-
Save MyKo101/d7d87022157410a40d02d6c6b4815592 to your computer and use it in GitHub Desktop.
Allow less thans in a chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| less_chain <- function(.a,a,b,equal=FALSE){ | |
| if(length(a) != length(b) && (length(a) != 1 && length(b) != 1)) | |
| stop("Incompatible lengths",call.=FALSE) | |
| if(is.call(.a) && | |
| (identical(.a[[1]],quote(`%<%`)) || identical(.a[[1]],quote(`%<=%`)))){ | |
| mid <- eval(.a[[3]]) | |
| rhs <- if(equal) mid <= b else mid < b | |
| a & rhs | |
| } else if(is.numeric(a) && is.numeric(b)){ | |
| if(equal) a <= b else a < b | |
| } | |
| } | |
| `%<%` <- function(a,b){ | |
| less_chain(substitute(a),eval(a),b,FALSE) | |
| } | |
| `%<=%` <- function(a,b){ | |
| less_chain(substitute(a),eval(a),b,TRUE) | |
| } | |
| x <- 1:10 | |
| 3 %<% x %<% 6 | |
| 6 %<=% x %<% 10 | |
| 1:2 %<% 2:4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment