Skip to content

Instantly share code, notes, and snippets.

@brooke-watson
Last active October 21, 2016 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brooke-watson/e03c8a785fe69cbef42a7ebfe9e24613 to your computer and use it in GitHub Desktop.
Save brooke-watson/e03c8a785fe69cbef42a7ebfe9e24613 to your computer and use it in GitHub Desktop.
'%=%' = function(l, r, ...)
UseMethod('%=%')
# Binary Operator
'%=%.lbunch' = function(l, r, ...) {
Envir = as.environment(-1)
if (length(r) > length(l))
warning("Right side has more args than left side. Only first ", length(l), " arguments used.")
if (length(l) > length(r)) {
warning("Left side has more arguments than right side. Right side will be repeated.")
r <- extendToMatch(r, l)
}
for (n in 1:length(l)) {
do.call('<-', list(l[[n]], r[[n]]), envir=Envir)
}
}
# Used if LHS is larger than RHS
extendToMatch <- function(source, destin) {
s <- length(source)
d <- length(destin)
# Assume that destin is a length when it is a single number and source is not
if(d==1 && s>1 && !is.null(as.numeric(destin)))
d <- destin
dif <- d - s
if (dif > 0) {
source <- rep(source, ceiling(d/s))[1:d]
}
return (source)
}
g = function(...) {
List = as.list(substitute(list(...)))[-1L]
class(List) = 'lbunch'
return(List)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment