Last active
May 28, 2017 06:03
-
-
Save zbjornson/c4b622e97e3d922292a482632909a50e to your computer and use it in GitHub Desktop.
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
hoistDotArgs = function(fn) { | |
dotCalls = list() | |
findDotArgs = function(exp) { | |
if (typeof(exp) == "closure") { # entry point | |
expL = as.list(exp) | |
args = names(expL) | |
if (!("..." %in% args)) return(invisible()) # no dots to hoist | |
body = expL[[length(expL)]] | |
findDotArgs(body) | |
} else if (typeof(exp) == "language") { | |
langL = as.list(exp) | |
if ("..." %in% langL) { | |
# print(exp) | |
fnameSym = langL[[1]] | |
fnameChar = as.character(fnameSym) | |
callee = get(fnameChar) # closure | |
args = names(as.list(callee)) | |
thisCall = list() | |
thisCall[[fnameChar]] = args | |
dotCalls <<- c(dotCalls, thisCall) | |
} else { | |
lapply(langL, function (x) { | |
findDotArgs(x) | |
}) | |
} | |
} | |
invisible() | |
} | |
findDotArgs(fn) | |
if (length(unique(names(dotCalls))) > 1) { | |
print("... passed to more than one different functions; could hoist common args") | |
} else { | |
dc = dotCalls[[1]] | |
fname = names(dotCalls)[[1]] | |
args = paste(head(dc, n = length(dc) - 1), collapse = ", ") | |
print(sprintf("... passed to %s. Hoist its arguments (%s)", fname, args)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
given:
then: