我正在尝试使用一系列lapply调用来构建curried函数列表,理想情况下,在最后一次lapply调用时,返回最终的期望值.currying工作,但lapply似乎总是应用第二个应用程序后列表中的最后一个元素.
例:
curry <- function(fn, ...) {
arglist <- list(...)
function(...) {
do.call(fn, append(arglist, list(...)))
}
}
# rcurry is used only to init the first lapply.
rcurry <- function(v1, fn, ...) {
arglist <- append(list(v1), list(...))
function(...) {
do.call(fn, append(arglist, list(...)))
}
}
myadd <- function(a,b,c) {
a+b+c
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作:
# you can achieve the same by closure:
# curry.a <- lapply(c(10, 1000), FUN = function(a) { curry(myadd, a) })
curry.a <- lapply(list(10, …Run Code Online (Sandbox Code Playgroud)