从闭包生成函数列表

Generate a list of functions from a closure

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

现在我希望 'funlist' 的第一个元素是平方函数,第二个是立方函数。然而,它似乎只是重复了 'cubing'.

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

有没有办法用这种方式从闭包中生成一个函数列表?或者围绕我可能想以这种方式处理的问题的一般最佳实践?

它在 3.2.0 中改变行为的原因是 news() 文件中的这个声明:

Higher order functions such as the apply functions and Reduce() now force arguments to the functions they apply in order to eliminate undesirable interactions between lazy evaluation and variable capture in closures. This resolves PR#16093.