迭代 - 解释和示例
有人可以解释一下Eteration到底是什么并举个例子吗?
来源:长时间运行的任务 Douglas Crockford 的 YUI 博客
Can someone explain what exactly is Eteration and show an example?
source: Long running tasks YUI blog by Douglas Crockford
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初,我认为这只是iteration的拼写错误,因为在线搜索eteration没有产生任何有意义的结果。
但是,然后,我发现参考文献指出该术语是由克罗克福德本人在一次演讲中。
在网上,我唯一能找到解释的地方是在他的页面上,阶乘教程,一篇文章,在第二幕中,作为对代码示例的评论,他指出:
这似乎是相关术语对的一部分,因为他的下一个代码示例(不使用堆栈执行递归)包含该术语对的另一个成员:
因此,似乎 eteration 和 ecursion 是 Crockford 自己发明和定义的术语,用于指代上下文中的消息迭代和递归。 E 编程语言,在 Java 之上为编写分布式应用程序的开发人员而设计。
该语言被称为 E 的事实也许是为其特定迭代和递归赋予所选术语(**e***teration* 和 **e***cursion*)的原因。
如果是 Javascript,Crockford 在演讲中解释了术语eteration Crockford on JavaScript -- 场景 6:Loopage,从 30:40 分钟开始:
结果是,迭代不是紧密循环(如果花费太长时间就会阻塞接口),而是在一个链中安排循环的每个步骤,仅在实际步骤执行时阻塞接口,不在步骤之间。这使得可以在与界面相同的线程中执行长时间运行的任务(Javascript 是单线程的),同时保持应用程序的响应能力。
查看质量更高的完整演讲,并附有全文记录 在这里。
此外,有关如何实现此类技术的参考,请考虑以下场景:
有两个
div
,一个显示正在进行的迭代的索引,另一个附加文本The每次按Try Me按钮时,界面仍然响应!。正如您从代码中看到的,迭代步骤是由setTimeout
安排的,间隔一定的时间间隔,从而允许用户交互的发生和处理。因此,当用户单击按钮并触发第二个 div 的更新时,迭代步骤将继续运行,保持页面的响应能力,同时完成其必须执行的工作的实际进展(在本例中,仅显示索引)。Initially, I thought it was just a typo of iteration, as searching online for eteration yields no significant results.
But, then, I came across references that state that the term is coined by Crockford himself, in one of his talks.
Online, the only place where I could find an explanation is on his page, in The Factorial Tutorial, an article where, in Act 2, as a comment to a code sample, he states:
This seems to be part of a related pair of terms, as his next code sample, that performs recursion without using a stack, contains the other member of the pair:
So, it seems that eteration and ecursion are terms invented and defined by Crockford himself to refer to message iteration and recursion in the context of the E Programming Language, designed on top of Java for developers who write distributed applications.
The fact that the language is called E is perhaps a reason to give its specific iteration and recursion flavors the chosen terminology (**e***teration* and **e***cursion*).
If the context of Javascript, Crockford explains the term eteration as part of the talk Crockford on JavaScript -- Scene 6: Loopage, starting from minute 30:40:
The result is that, instead of a tight loop, blocking the interface if it takes too long, the eteration schedules each step of the loop, in a chain that only blocks the interface while the actual step executes, not between steps. This makes it possible to perform long-running tasks in the same thread as the interface (Javascript is single-threaded), while maintaining application responsiveness.
Check out the full talk in much better quality and accompanied by a full-text transcript here.
Also, for a reference on how such a technique might be implemented, consider the following scenario:
There are two
div
s, one displaying the indices of the ongoing eteration, the other appending the text The Interface is Still Responsive! on each Try Me button press. As you can see from the code, the eteration steps are scheduled bysetTimeout
some time interval apart, allowing for user interaction to take place and be processed as well. Thus, the eteration steps will continue to run as the user clicks on the button and triggers the update of the second div, maintaining the page's responsiveness while doing real progress with the work it has to perform (in this case, simply displaying indices).