Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

list of lambdas in R

I want to make an R function that returns a list of functions that each operate on a different part of a list. However, because of R's rules on scope, this seems impossible. Here's an example:

functiontest = function() {
    foo = list()
    for(i in 1:3) {
        fixer = function(s) { return(
            function() {
                return(s)
            }
        )}
        foo[[i]] = fixer(i)
    }
    return(foo)
}

functiontest()[[2]]() #returns 3

Even killing off the named lambda "fixer" and using an immediate function doesn't save me:

functiontest = function() {
    foo = list()
    for(i in 1:3) {
        foo[[i]]=(function(s) { return(
            function() {
                return(s)
            }
        )})(i)
    }
    return(foo)
}

functiontest()[[2]]() #returns 3

I want this to return 2. How can I refactor this code so that this will happen?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, the problem you have faced has nothing to do with the scoping. Second, I don't think it is a good idea to call something idiotic just because you don't understand it.

In your example, all functions in a list return the same value because of the lazy evaluation mechanism in R. See help(force), which specifically addresses your question. In short, you need to force evaluation of the function at the time you call it, which can be done by adding force:

functiontest = function() {
    foo = list()
    for(i in 1:3) {
        fixer = function(s) { 
            force(s) ### <<<---This is the only difference from your code ###
            return(
            function() {
                return(s)
            }
        )}
        foo[[i]] = fixer(i)
    }
    return(foo)
}

or, using a more concise syntax:

functiontest <- function()  lapply(1:3, function(s) {s; function() s})

Example of use:

> L <- functiontest()

> L[[1]]()
[1] 1

> L[[2]]()
[1] 2

> L[[3]]()
[1] 3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...