Created
September 6, 2013 23:49
-
-
Save rpietro/6471450 to your computer and use it in GitHub Desktop.
metaprogramming example from http://goo.gl/fmIh4T
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
# script from http://goo.gl/fmIh4T | |
myFun <- function(vec) { | |
numElements <- length(which(vec > threshold)) | |
numElements | |
} | |
genMyFuns <- function(thresholds) { | |
ll <- length(thresholds) | |
print("Generating functions:") | |
for(i in 1:ll) { | |
fName <- paste("myFun.", i, sep="") | |
print(fName) | |
assign(fName, eval( | |
substitute( | |
function(vec) { | |
numElements <- length(which(vec > tt)); | |
numElements; | |
}, | |
list(tt=thresholds[i]) | |
) | |
), | |
envir=parent.frame() | |
) | |
} | |
} | |
genMyFuns(c(7, 9, 10)) | |
myFun.1(1:20) | |
myFun.2(1:20) | |
myFun.3(1:20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment