有两个函数访问单个函数环境的正确方法是什么?

Car*_*oft 6 scope r

根据1088639中提供的答案,我设置了一对函数,它们都访问相同的子函数环境.这个例子有效,但我想看看我是否错过了将两个顶层函数"连接"到内部环境的更简洁方法.

(背景故事:我想编写一对共享变量的互补函数,例如本例中的"count",满足CRAN包要求,这些要求不允许函数修改全局环境.)

static.f <- function() {
    count <- 0
    f <- function(x) {
        count <<- count + 1
        return( list(mean=mean(x), count=count) )
    }
    return( f )
}

#  make sure not to delete this command, even tho' it's not
# creating a function.
f1 <- static.f()

statfoo <- function(x){
    tmp<-f1(x)
    tmp<- list(tmp,plus=2)
    return(tmp)
}
statbar <- function(x){
    tmp<-f1(x)
    tmp<- list(tmp,minus=3)
    return(tmp)
}
Run Code Online (Sandbox Code Playgroud)

样本输出:

> statfoo(5)
[[1]]
[[1]]$mean
[1] 5

[[1]]$count
[1] 1

$plus
[1] 2

Rgames> statfoo(5)
[[1]]
[[1]]$mean
[1] 5

[[1]]$count
[1] 2

$plus
[1] 2

> statbar(4)
[[1]]
[[1]]$mean
[1] 4

[[1]]$count
[1] 3

$minus
[1] 3

> statfoo(5)
[[1]]
[[1]]$mean
[1] 5

[[1]]$count
[1] 4

$plus
[1] 2
Run Code Online (Sandbox Code Playgroud)

G. *_*eck 5

更简洁的方法是使用面向对象的方法.已经有一个使用参考类的答案.

使用类的典型面向对象方法将创建一个类,然后创建一个单例对象,即该类的单个对象.当然创建一个类只是为了从它创建一个对象有点浪费,所以这里我们提供一个proto示例.(创建一个封闭count的函数和执行实际工作的函数有一个类似的问题 - 你创建一个封闭函数只运行一次.)proto模型允许一个人创建一个对象直接绕过创建一个类只需要使用它一次.这foobar是具有属性count和方法的proto对象stats,statfoo以及statbar.请注意,我们考虑stats到避免在每个statfoo和中复制其代码statbar.(继续向下)

library(proto)

foobar <- proto(count = 0, 
          stats = function(., x) {
               .$count <- .$count + 1
               list(mean = mean(x), count = .$count)
          },
          statfoo = function(., x) c(.$stats(x), plus = 2),
          statbar = function(., x) c(.$stats(x), plus = 3)
)

foobar$statfoo(1:3)
foobar$statbar(2:4)
Run Code Online (Sandbox Code Playgroud)

赠送:

> foobar$statfoo(1:3)
$mean
[1] 2

$count
[1] 1

$plus
[1] 2

> foobar$statbar(2:4)
$mean
[1] 3

$count
[1] 2

$plus
[1] 3
Run Code Online (Sandbox Code Playgroud)

第二个设计方案将有statfoostatbar作为独立的功能,只保留countstatsfoobar(续进一步下跌)

library(proto)

foobar <- proto(count = 0, 
          stats = function(., x) {
               .$count <- .$count + 1
               list(mean = mean(x), count = .$count)
          }
)

statfoo <- function(x) c(foobar$stats(x), plus = 2)
statbar <- function(x) c(foobar$stats(x), plus = 3)

statfoo(1:3)
statbar(2:4)
Run Code Online (Sandbox Code Playgroud)

给前面的例子提供类似的输出.

第三种方法当然,第二种变体可以通过使用local和使我们接近您开始的位置的函数轻松实现.这不会使用任何包,但不会创建一个函数只是扔掉它:

foobar <- local({
            count <- 0
            function(x) {
               count <<- count + 1
               list(mean = mean(x), count = count)
            }
          })

statfoo <- function(x) c(foobar(x), plus = 2)
statbar <- function(x) c(foobar(x), plus = 3)

statfoo(1:3)
statbar(2:4)
Run Code Online (Sandbox Code Playgroud)


MrF*_*ick 3

您可以摆脱工厂函数,并更明确地使用环境。像这样的解决方案也可以工作

.env<-(function() {
    count <- 0
    f <- function(x)  {
        count <<- count + 1
        return( list(mean=mean(x), count=count))
    }   
    return(environment())
})()



statfoo <- function(x){
    list(.env$f(x),plus=2)
}
statbar <- function(x){
    list(.env$f(x),minus=3)
}
Run Code Online (Sandbox Code Playgroud)

.env变量是通过立即执行匿名函数来获取其环境来创建的。然后我们从环境本身中提取函数来修改它的值。