Hi. I would like to partially apply a function to a list of arguments, and I don't know how to do this in R, without perhaps writing default values to the formals() of my function, or writing to the environment object of a function. For context, my definition of partially apply is: "fix some of the arguments, leaving the others as variables, return a new function that takes the un-fixed arguments from the original function as arguments" The issue is that I would like several different sets of arguments for the "partially applied" phase, and I think that would involve copying the function to several different places with the above methods. Alternately, I could hard-code the partial applications using a function() form, but I would prefer not to do this, as there will be a sizable number of these closures needed and I'd prefer to automate the process. Thanks! --Mog
On Aug 6, 2010, at 9:56 AM, Mog wrote:> Hi. I would like to partially apply a function to a list of arguments, > and I don't know how to do this in R, without perhaps writing default > values to the formals() of my function, or writing to the environment > object of a function. For context, my definition of partially apply > is: "fix some of the arguments, leaving the others as variables, > return a new function that takes the un-fixed arguments from the > original function as arguments"I don't understand the difficulties: > f1 <- function(a,b,c,d) a+b+c+d > f1(1,2,3,4) [1] 10 > f2 <- function(a , b) f1(a,b, 3,4) > f2(1,2) [1] 10 > f1(1,2) Error in a + b + c : 'c' is missing > floop <- function(a,b){ for( cc in 2:4) { for (dd in 5:7) { print( f1(a, b, cc, dd) )}} } > floop(1,2) [1] 10 [1] 11 [1] 12 [1] 11 [1] 12 [1] 13 [1] 12 [1] 13 [1] 14> > The issue is that I would like several different sets of arguments for > the "partially applied" phase, and I think that would involve copying > the function to several different places with the above methods. > > Alternately, I could hard-code the partial applications using a > function() form, but I would prefer not to do this, as there will be a > sizable number of these closures needed and I'd prefer to automate the > process. > > Thanks! > > --Mog > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Hi, I think you mean Currying, there's a function in roxygen that looks like, Curry function (f, ..., .left = TRUE) { .orig = list(...) function(...) { if (.left) { args <- c(.orig, list(...)) } else { args <- c(list(...), .orig) } do.call(f, args) } } HTH, baptiste On Aug 6, 2010, at 3:56 PM, Mog wrote:> Hi. I would like to partially apply a function to a list of arguments, > and I don't know how to do this in R, without perhaps writing default > values to the formals() of my function, or writing to the environment > object of a function. For context, my definition of partially apply > is: "fix some of the arguments, leaving the others as variables, > return a new function that takes the un-fixed arguments from the > original function as arguments" > > The issue is that I would like several different sets of arguments for > the "partially applied" phase, and I think that would involve copying > the function to several different places with the above methods. > > Alternately, I could hard-code the partial applications using a > function() form, but I would prefer not to do this, as there will be a > sizable number of these closures needed and I'd prefer to automate the > process. > > Thanks! > > --Mog > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.