r - Generate a list of functions from a closure -


power <- function(exp) {   function(x) {     x ^ exp   } } funlist <- lapply(2:3, power) 

now expect first element of 'funlist' square function , second cube function. seems repeats 'cubing'.

> funlist[[1]](5) [1] 125 > funlist[[2]](5) [1] 125 

is there way generate list of functions closure in way? or general best practices surrounding problem may want approach in way?

the reason changed behavior in 3.2.0 announcement in news() file:

higher order functions such apply functions , reduce() force arguments functions apply in order eliminate undesirable interactions between lazy evaluation , variable capture in closures. resolves pr#16093.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -