解释一个懒惰的评估怪癖 [英] Explain a lazy evaluation quirk

查看:93
本文介绍了解释一个懒惰的评估怪癖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hadley Wickhams关于Github的书,尤其是这部分内容懒惰的评估.在此,他在带有add/adders函数的部分中给出了延迟评估的后果的示例.让我引用一下:

I am reading Hadley Wickhams's book on Github, in particular this part on lazy evaluation. There he gives an example of consequences of lazy evaluation, in the part with add/adders functions. Let me quote that bit:

在创建带有lapply或循环的闭包时,此[惰性评估]很重要:

This [lazy evaluation] is important when creating closures with lapply or a loop:

add <- function(x) {
  function(y) x + y
}
adders <- lapply(1:10, add)
adders[[1]](10)
adders[[10]](10)

第一次调用加法器时,

x会被延迟评估 功能.至此,循环完成,最终值 x是10.因此所有加法器函数都会在其加法器上加10 输入,可能不是您想要的!手动强制评估修复 问题:

x is lazily evaluated the first time that you call one of the adder functions. At this point, the loop is complete and the final value of x is 10. Therefore all of the adder functions will add 10 on to their input, probably not what you wanted! Manually forcing evaluation fixes the problem:

add <- function(x) {
  force(x)
  function(y) x + y
}
adders2 <- lapply(1:10, add)
adders2[[1]](10)
adders2[[10]](10)

我似乎不太了解这一点,这里的解释很少.有人可以详细说明该特定示例,并解释那里发生的情况吗?我特别对句子到此为止,循环已完成且x的最终值为10"感到困惑.什么循环?什么最终值在哪里?一定是简单的东西,我很想念,但我只是看不到.提前非常感谢.

I do not seem to understand that bit, and the explanation there is minimal. Could someone please elaborate that particular example, and explain what happens there? I am specifically puzzled by the sentence "at this point, the loop is complete and the final value of x is 10". What loop? What final value, where? Must be something simple I am missing, but I just don't see it. Thanks a lot in advance.

推荐答案

目标:

adders <- lapply(1:10, function(x)  add(x) )

是创建add函数的列表,第一个添加1,第二个添加2,依此类推.延迟求值使R等待真正创建加法器函数,直到您真正开始调用这些函数为止.问题在于,在创建第一个加法器函数后,x将通过lapply循环增加,最终以值10结束.当您调用第一个加法器函数时,惰性求值现在将构建该函数,并获得x.问题在于原始的x不再等于1,而是等于lapply循环末尾的值,即10.

is to create a list of add functions, the first adds 1 to its input, the second adds 2, etc. Lazy evaluation causes R to wait for really creating the adders functions until you really start calling the functions. The problem is that after creating the first adder function, x is increased by the lapply loop, ending at a value of 10. When you call the first adder function, lazy evaluation now builds the function, getting the value of x. The problem is that the original x is no longer equal to one, but to the value at the end of the lapply loop, i.e. 10.

因此,延迟求值导致所有加法器函数都等到lapply循环完成之后才真正构建该函数.然后他们以相同的值(即10)构建函数.Hadley建议的解决方案是强制x直接求值,避免延迟求值,并使用正确的x值获得正确的函数.

Therefore, lazy evaluation causes all adder functions to wait until after the lapply loop has completed in really building the function. Then they build their function with the same value, i.e. 10. The solution Hadley suggests is to force x to be evaluated directly, avoiding lazy evaluation, and getting the correct functions with the correct x values.

这篇关于解释一个懒惰的评估怪癖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆