避免全局变量 [英] Avoiding Global Variables
问题描述
我想制作一个基本的分析工具,收集时间戳并用笔记产生运行时间。唯一的问题是我无法弄清楚如何在不使用全局变量的情况下做到这一点。实现我试图实现的功能的正确方式是什么?如果R已经内置了这个功能,那真是太棒了,但我真正想知道的是如何避免使用全局变量并编写更健壮的代码。
<$ ($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$' )< 1){
timeStampps<< - 系统时间()
}
else {
timeStamps<< - c(timeStamps,Sys.time ))
diff< - timeStamps [length(timeStamps)] - timeStamps [length(timeStamps) - 1]
runTimes<< -c(runTimes,format(diff))
名称(runTimes)[长度(runTimes)]<< - note
}
}
appendRunTimes('start')
Sys.sleep(4)
appendRunTimes('test')
RTmonitor< - local({
timeStamps = c()
runTimes = list()
list(
appendRunTimes = function(note){
if(length(timeStamps)< 1){
timeStamps < - - Sys.time()
}
else {
timeStamps < - c(timeStamps,Sys.time())
diff < - timeStamps [length(timeStamps)] - timeStamps [length(timeStamps) - 1]
runTimes <-c(runTimes,format(diff))
names(runTimes )[length(runTimes)]<< - note
}
},
viewRunTimes = function(){
return(list(timeStamps = timeStamps,runTimes = runTimes) )
})
})
> RTmonitor $ appendRunTimes(start)
> RTmonitor $ appendRunTimes(test)
> RTmonitor $ viewRunTimes()
$ timeStamps
[1]2013-01-04 18:39:12 EST2013-01-04 18:39:21 EST
$ runTimes
$ runTimes $ test
[1]8.855587 secs
观察值存储在闭包中,而不是在全局环境中:
> timeStamps
错误:找不到对象'timeStamps'
> runTimes
错误:未找到对象'runTimes'
> RTmonitor $ timeStamps
NULL
> RTmonitor $ runTimes
NULL
关于闭包和避免全局变量的更多阅读:
I'd like to make a basic profiling tool that collects time stamps and produces run times with a note. The only problem is I'm having trouble figuring out how to do this without using global variables. What's the "correct" way to implement the functionality I'm trying to achieve? If R already has this functionality built in, that's awesome, but what I'm really trying to figure out here is how to avoid using global variables and write more robust code.
timeStamps = c()
runTimes = list()
appendRunTimes <- function(note) {
if(length(timeStamps) < 1) {
timeStamps <<- Sys.time()
}
else {
timeStamps <<- c(timeStamps, Sys.time())
diff <- timeStamps[length(timeStamps) ] - timeStamps[length(timeStamps) - 1]
runTimes <<- c(runTimes, format(diff))
names(runTimes)[length(runTimes)] <<- note
}
}
appendRunTimes('start')
Sys.sleep(4)
appendRunTimes('test')
Here's your example rewritten using closures:
RTmonitor <- local({
timeStamps = c()
runTimes = list()
list(
appendRunTimes=function(note) {
if(length(timeStamps) < 1) {
timeStamps <<- Sys.time()
}
else {
timeStamps <<- c(timeStamps, Sys.time())
diff <- timeStamps[length(timeStamps) ] - timeStamps[length(timeStamps) - 1]
runTimes <<- c(runTimes, format(diff))
names(runTimes)[length(runTimes)] <<- note
}
},
viewRunTimes=function() {
return(list(timeStamps=timeStamps,runTimes=runTimes))
})
})
> RTmonitor$appendRunTimes("start")
> RTmonitor$appendRunTimes("test")
> RTmonitor$viewRunTimes()
$timeStamps
[1] "2013-01-04 18:39:12 EST" "2013-01-04 18:39:21 EST"
$runTimes
$runTimes$test
[1] "8.855587 secs"
Observe that the values are stored inside the closure, not in the global environment:
> timeStamps
Error: object 'timeStamps' not found
> runTimes
Error: object 'runTimes' not found
> RTmonitor$timeStamps
NULL
> RTmonitor$runTimes
NULL
More reading on closures and avoiding globals:
- Closures as solution to data merging idiom
- How does local() differ from other approaches to closure in R?
- Why is using `<<-` frowned upon and how can I avoid it?
- Examples of the perils of globals in R and Stata
这篇关于避免全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!