关闭的执行顺序 [英] Sequence of execution for closures
问题描述
这是我创建的一个测试函数,用于了解 lapply
和闭包之间的交互方式.我的目标是依次遍历 runif
函数并打印每次迭代.
Here's a test function I created to learn how lapply
and closures interact. My objective is to iterate over runif
function sequentially and print each iteration.
Mod_delay_by <- function(delay,f){
function(x,...){
Sys.sleep(delay)
cat(".")
#f(x,...)
cat("Processing",x,"\n",sep = "")
f(x,...)
}
}
现在让我们使用 lapply
来调用上述函数:
Now let's use lapply
to call above function:
lapply(1:3,Mod_delay_by(0.1,runif))
当我调用上面的函数时,我得到以下示例输出:
When I call above function, I get the following sample output:
.Processing1
.Processing2
.Processing3
[[1]]
[1] 0.835246
[[2]]
[1] 0.1370997 0.4350032
[[3]]
[1] 0.1174749 0.4087628 0.7222604
我很惊讶,因为所有三个".Processing"都在执行 runif
之前出现.相反,我希望输出为...
I am surprised because all the three ".Processing" come before execution of runif
. Instead, I would have expected the output to be ...
.Processing1
[[1]]
[1] 0.835246
.Processing2
[[2]]
[1] 0.1370997 0.4350032
.Processing3
[[3]]
[1] 0.1174749 0.4087628 0.7222604
...因为 runif
(即函数 f
)前面紧跟着对".Processing"的调用
...because runif
(i.e. function f
) is immediately preceded by a call to ".Processing"
有人可以解释为什么 lapply
首先完成对所有三个 Processing
的打印,然后再对所有三个 runifs
进行打印吗?
Can someone please explain why lapply
first finishes printing all the three Processing
, and then prints all three runifs
?
在此先感谢您的帮助.
推荐答案
很显然,OP希望将 f()
的输出打印到控制台窗口中.每个调用都像 cat()
一样.
Apparently, the OP expects the output of f()
should be printed to the console window in each call like cat()
does.
类似于直接从控制台窗口调用函数时会发生的情况:
Similar to what happens when the function is called directly from the console window:
> Mod_delay_by(0.1, runif)(2)
.Processing2
[1] 0.4519318 0.2331198
请注意,如果将输出分配给变量,我们将看不到任何打印内容( cat()
的输出除外)
Please note that we won't see anything printed (except the output of cat()
) if the output is assigned to a variable:
> result <- Mod_delay_by(0.1, runif)(2)
.Processing2
因此,如果您从控制台窗口调用函数,并且输出 not 未分配给变量,则会将其输出到控制台窗口.
So, if you call a function from the console window and the output is not assigned to a variable it is printed to the console window.
现在,正在从函数体内调用 f()
.这种情况是不同的.
Now, f()
is being called from within the body of a function. This situation is different.
f()
的输出不是打印到控制台窗口,而是返回给调用者.帮助文件?"function"
说,如果在不调用 return
的情况下到达了函数的末尾,则返回最后一个求值表达式的值.
Instead of being printed to the console window, the output of f()
is returned to the caller. The help file ?"function"
says If the end of a function is reached without calling return
, the value of the last evaluated expression is returned.
这可以通过编写来变得更明确
This can be made more explicit by writing
return(f(x, ...))
因此,只有 cat()
打印到控制台. f()
的输出返回到调用 lapply()
函数,该函数将所有输出收集在一个列表中.最后,此列表仅由于未分配给变量而被打印到控制台.
So, only cat()
prints to console. The output of f()
is returned to the calling lapply()
function which collects all output in a list. Finally, this list is printed to the console only because it is not assigned to a variable.
如果将输出分配给变量,则除了打印到控制台的 cat()
之外,我们看不到其他任何内容.
If the output is assigned to a variable, we won't see anything printed except what cat()
prints to the console.
> result <- lapply(1:3, Mod_delay_by(0.1, runif))
.Processing1
.Processing2
.Processing3
总而言之,R尝试保存类型,但结果取决于上下文:
To summarize, R is trying to save typing but the outcome depends on the context:
-
在控制台窗口中,未分配的
f()
表示print(f())
.
在函数主体中, f()
作为最后一个求值表达式表示 return(f())
.
In the body of a function, f()
as last evaluated expression implies return(f())
.
这篇关于关闭的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!