迭代 - 解释和示例

发布于 2024-12-01 02:08:03 字数 186 浏览 2 评论 0原文

有人可以解释一下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-12-08 02:08:03

最初,我认为这只是iteration的拼写错误,因为在线搜索eteration没有产生任何有意义的结果。

但是,然后,我发现参考文献指出该术语是由克罗克福德本人在一次演讲中。
在网上,我唯一能找到解释的地方是在他的页面上,阶乘教程,一篇文章,在第二幕中,作为对代码示例的评论,他指出:

Act 2a:消息迭代(eteration)

这似乎是相关术语对的一部分,因为他的下一个代码示例(不使用堆栈执行递归)包含该术语对的另一个成员:

Act 2b:消息递归(ecursion)

因此,似乎 eterationecursion 是 Crockford 自己发明和定义的术语,用于指代上下文中的消息迭代和递归。 E 编程语言,在 Java 之上为编写分布式应用程序的开发人员而设计。

该语言被称为 E 的事实也许是为其特定迭代和递归赋予所选术语(**e***teration* 和 **e***cursion*)的原因。

如果是 Javascript,Crockford 在演讲中解释了术语eteration Crockford on JavaScript -- 场景 6:Loopage,从 30:40 分钟开始:

迭代意味着将一个任务分成多个回合,以便在每个回合中
迭代,而不是通过传统的循环,在底部
我们调用 setTimeOut 循环,向其传递一个函数,该函数会导致
我们进行下一次迭代。这意味着转弯将是
很短——这一轮只有一次那么长——我们可以这样做
根据我们的需要进行多次迭代,而不是锁定事件循环。

结果是,迭代不是紧密循环(如果花费太长时间就会阻塞接口),而是在一个链中安排循环的每个步骤,仅在实际步骤执行时阻塞接口,不在步骤之间。这使得可以在与界面相同的线程中执行长时间运行的任务(Javascript 是单线程的),同时保持应用程序的响应能力。

查看质量更高的完整演讲,并附有全文记录 在这里

此外,有关如何实现此类技术的参考,请考虑以下场景:

<html>
 <head>
  <script type="text/javascript">
   function testFeedback()
   {
      var feedbackDiv = document.getElementById("feedbackDiv");

      feedbackDiv.innerHTML += "The Interface is Still Responsive!</br>";
   }

   var currentNumber = 0;
   var loopStepDelay = 30;

   function performLoopStep()
   {
     var numbersDiv = document.getElementById("numbersDiv");
     numbersDiv.innerHTML = currentNumber++;
     setTimeout("performLoopStep()", loopStepDelay);
   }
   setTimeout("performLoopStep()", loopStepDelay);
  </script>
 </head>
 <body>
  <div id="numbersDiv"></div>
  </br>
  </br>
  <div id="feedbackDiv"></div>
  </br>
  </br>
  <button onClick="testFeedback()">Try Me</button>
 <body>
</html>

有两个 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:

Act 2a: message iteration (eteration)

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:

Act 2b: message recursion (ecursion)

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:

Eteration means to break a task into multiple turns so that on each
eteration, instead of going through a conventional loop, at the bottom
of the loop we call setTimeOut, passing it a function which causes
us to do the next eteration. That means that the turns are going to be
short — the turn's only as long as one eteration – and we can do as
many eterations as we want and not lock up the event loop.

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:

<html>
 <head>
  <script type="text/javascript">
   function testFeedback()
   {
      var feedbackDiv = document.getElementById("feedbackDiv");

      feedbackDiv.innerHTML += "The Interface is Still Responsive!</br>";
   }

   var currentNumber = 0;
   var loopStepDelay = 30;

   function performLoopStep()
   {
     var numbersDiv = document.getElementById("numbersDiv");
     numbersDiv.innerHTML = currentNumber++;
     setTimeout("performLoopStep()", loopStepDelay);
   }
   setTimeout("performLoopStep()", loopStepDelay);
  </script>
 </head>
 <body>
  <div id="numbersDiv"></div>
  </br>
  </br>
  <div id="feedbackDiv"></div>
  </br>
  </br>
  <button onClick="testFeedback()">Try Me</button>
 <body>
</html>

There are two divs, 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 by setTimeout 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文