在lapply / R承诺 [英] Promises in lapply / R
问题描述
我不知道什么是承诺中的R做
如果持斧
A = lapply(seq_len(2),功能(N){函数(){N}})
B = lapply(seq_len(2),功能(N){N})
我们可以看到,
A [[1]]()==#2
B〔[1]]#== 1
据我所知,R使用承诺的对象并懒洋洋地评估在其环境中的前pression,但我不明白,为什么不同的环境为每个函数创建中不含有对n自身的价值。
[[1]]
功能()
{
ñ
}
<环境:0x7f9b2416ad18>[[2]
功能()
{
ñ
}
<环境:0x7f9b2416ab20>as.list(环境(一个[[1]]))
$ N
[1] 2as.list(环境(一个[[2]]))
$ N
[1] 2
时,才有可能通过lapply功能来解决语义不知何故?
lapply
功能(X,FUN,...)
{
FUN< - match.fun(FUN)
如果(!is.vector(Ⅹ)|| is.object(X))
X'LT; - as.list(X)
。内部(lapply(X,FUN))
}
<字节code:0x7f9b25150f18>
<环境:命名空间:基地>
PS:重新调整的问题。
编辑:具体来说,有可能写的 lapply2 函数一般势力的说法有统一的行为,如下所示:
PL< - lapply(1:3,功能(Y){力(Y);函数(x)的POW(X,Y)})
PL< - lapply2(1:3,功能(Y){函数(x)的POW(X,Y)})
我觉得它更容易在这种形式的理解:
F =功能(N){函数(){N}}
X = 1
一个= F(X)
X = 2
一个()
[1] 2
文件的关键部分是
当一个函数被调用的参数匹配,然后每个
形式参数绑定到一个承诺。这位前pression这是
对于正式的参数和一个指向环境中给出的
功能是从被存储在许称为
块引用>呼叫后
A = F(X)
,函数参数N
绑定与承诺名称X
和一个指向全球环境.GlobalEnv
。在你的
lapply
例子中,匿名函数功能(N){函数(){N}}
是从全球环境的每次调用。这就是为什么在列表中的每个元素A
获得的相同值n
:它是从全球环境未来。我不明白它是如何可能通过改写lapply改变这种行为。I am not sure what the promises are doing in R
If one runs
a = lapply(seq_len(2), function(n) { function() {n}}) b = lapply(seq_len(2), function(n) {n})
we can see that
a[[1]]() # == 2 b[[1]] # == 1
I understand that R uses promise's object and lazily evaluates an expression in its environment, but I dont understand why the different environments created for each function would not contain their own value for n.
[[1]] function () { n } <environment: 0x7f9b2416ad18> [[2]] function () { n } <environment: 0x7f9b2416ab20> as.list(environment(a[[1]])) $n [1] 2 as.list(environment(a[[2]])) $n [1] 2
Is it be possible to fix the semantic through the lapply function somehow ?
lapply function (X, FUN, ...) { FUN <- match.fun(FUN) if (!is.vector(X) || is.object(X)) X <- as.list(X) .Internal(lapply(X, FUN)) } <bytecode: 0x7f9b25150f18> <environment: namespace:base>
PS : refocused question
Edit : concretely, is it possible to write a lapply2 function that generically "forces" the argument to have uniform behaviour as in :
pl <- lapply (1:3, function(y) { force(y); function(x) pow(x,y) } ) pl <- lapply2(1:3, function(y) { function(x) pow(x,y) } )
解决方案I find it easier to understand in this form:
f=function(n) {function() {n}} x=1 a=f(x) x=2 a() [1] 2
The key part of the documentation is
When a function is called the arguments are matched and then each of the formal arguments is bound to a promise. The expression that was given for that formal argument and a pointer to the environment the function was called from are stored in the promise.
After the call
a=f(x)
, the function argumentn
is bound to a promise with the namex
and a pointer to the global environment.GlobalEnv
.In your
lapply
examples, the anonymous functionfunction(n) { function() {n}}
is called from the global environment each time. This is why every element of the lista
gets the same value ofn
: it's coming from the global environment. I don't see how it's possible to change this behaviour by rewriting lapply.这篇关于在lapply / R承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!