Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...