【发布时间】:2014-08-19 13:04:31
【问题描述】:
如果我有一个函数发生器,即
prepend_text = function(text) {
function(x) {
paste(text, x)
}
}
是否可以使用 vapply 返回结果函数的向量?我不知道如何为 vapply 指定返回类型为 closure。
vapply(c('one', 'two', 'three'), FUN.VALUE=???, prepend_text)
使用任何其他值 character(1) 等。产量
# Error in vapply(c('one', 'two', 'three'), :
# values must be type 'character',
# but FUN(X[[1]]) result is type 'closure'
我认为这可能是不可能的,使用sapply 实际上会返回我想要的,所以这个问题主要是学术性的。
我要查找的输出也包含每个函数的名称,因此简单的lapply 调用也不起作用。
预期输出等价于
c(one=prepend_text('one'), two=prepend_text('two'), three=prepend_text('three'))
【问题讨论】:
标签: r