Skip to content

Instantly share code, notes, and snippets.

@MyKo101
Created April 18, 2021 20:27
Show Gist options
  • Select an option

  • Save MyKo101/d7d87022157410a40d02d6c6b4815592 to your computer and use it in GitHub Desktop.

Select an option

Save MyKo101/d7d87022157410a40d02d6c6b4815592 to your computer and use it in GitHub Desktop.
Allow less thans in a chain
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