[R] functional (?) programming in r

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Nov 18 11:22:18 CET 2008


> On Mon, Nov 17, 2008 at 7:33 PM, Wacek Kusnierczyk
>
>   
>
>> ...what i would (r-naively) expect is that lapply for-loops over an index variable, and each promise picks from the list a value at that index....
>>     


let me make this line more clear:

funcs = lapply(1:2, function(i) function() i)

would make an environment in which 1:2 would be bound to some variable
(say, v) and each closure in funcs would have in its own closure
environment an unevaluated promise with an expression involving the
variable (v) and an index *value*.  that is, funcs[[1]] would have a
promise with the expression "v[[1]]"; when forced, the promise would get
the value 1 (since v is c(1,2), and v[[1]] is 1).

this would make for one way to solve my original problem (without
forcing me to force the promises myself).  but this depends on an
assumption on how lapply works, which seems inaccurate.  the other
solution (where the promises corresponding to i inside each closure are
forced when the closure is created) appears better, but this is
guesswork which needs careful examination.

vQ



More information about the R-help mailing list