Promises in lapply / R

前端 未结 2 1044
难免孤独
难免孤独 2021-01-05 20:44

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)           


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 21:07

    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 argument n is bound to a promise with the name x and a pointer to the global environment .GlobalEnv.

    In your lapply examples, the anonymous function function(n) { function() {n}} is called from the global environment each time. This is why every element of the list a gets the same value of n: it's coming from the global environment. I don't see how it's possible to change this behaviour by rewriting lapply.

提交回复
热议问题