[R] mapply & assign to generate functions
J Toll
jctoll at gmail.com
Mon Mar 12 18:47:10 CET 2012
Hi,
I have a problem that I'm finding a bit tricky. I'm trying to use
mapply and assign to generate curried functions. For example, if I
have the function divide
divide <- function(x, y) {
x / y
}
And I want the end result to be functionally equivalent to:
half <- function(x) divide(x, 2)
third <- function(x) divide(x, 3)
quarter <- function(x) divide(x, 4)
But I want to do it using mapply and assign:
mapply(assign,
c("half", "third", "quarter"),
lapply(2:4, function(i) {function(x) divide(x, i)}),
pos = 1)
The problem is in the third line. I end up with 3 functions that are
all functionally equivalent to quarter(). Any suggestions on how to
get this to work properly.
Thanks.
James
More information about the R-help
mailing list