0

I have a function of two parameters, and from that I'd like to create a list of functions in one parameter, like this:

flist <- lapply(c(1,2), function(x) { function(y) x + y })

This is intended to create a list of two functions, the first of which adds one to its input; the second of which adds two to its input. But it doesn't work:

> flist[[1]](3)
[1] 5
> flist[[2]](3)
[1] 5

Both of the functions in the list add two to their input. I have workarounds, but they are ugly, and I'd like to understand what I'm doing wrong.

grbruns
  • 61
  • 6
  • 1
    http://stackoverflow.com/questions/12481404/how-to-create-a-vector-of-functions – Peyton Sep 27 '13 at 02:41
  • Thanks! I now have `{ force(x); function(y) x + y }` instead of just `{ function(y) x + y }`, and I get what I want. – grbruns Sep 27 '13 at 02:46

0 Answers0