什么是 Scala 延续,为什么要使用它们? [英] What are Scala continuations and why use them?

查看:33
本文介绍了什么是 Scala 延续,为什么要使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了在 Scala 中编程,并且我已经一直在研究 Scala 2.7 和 2.8 之间的变化.似乎最重要的是 continuation 插件,但我不明白它有什么用处或它是如何工作的.我已经看到它对异步 I/O 有好处,但我一直无法找出原因.有关该主题的一些较受欢迎的资源如下:

I just finished Programming in Scala, and I've been looking into the changes between Scala 2.7 and 2.8. The one that seems to be the most important is the continuations plugin, but I don't understand what it's useful for or how it works. I've seen that it's good for asynchronous I/O, but I haven't been able to find out why. Some of the more popular resources on the subject are these:

关于 Stack Overflow 的这个问题:

And this question on Stack Overflow:

不幸的是,这些参考文献中没有一个试图定义延续的用途或移位/重置函数应该做什么,而且我还没有找到任何可以做到的参考文献.我无法猜测链接文章中的任何示例是如何工作的(或它们的作用),因此帮助我的一种方法可能是逐行浏览其中一个示例.即使是第三篇文章中的这个简单的:

Unfortunately, none of these references try to define what continuations are for or what the shift/reset functions are supposed to do, and I haven't found any references that do. I haven't been able to guess how any of the examples in the linked articles work (or what they do), so one way to help me out could be to go line-by-line through one of those samples. Even this simple one from the third article:

reset {
    ...
    shift { k: (Int=>Int) =>  // The continuation k will be the '_ + 1' below.
        k(7)
    } + 1
}
// Result: 8

为什么结果是8?这可能会帮助我开始.

Why is the result 8? That would probably help me to get started.

推荐答案

我的 博客 确实解释了 resetshift 的作用,所以你可能想再读一遍.

My blog does explain what reset and shift do, so you may want to read that again.

另一个很好的来源,我也在我的博客中指出,是关于延续传递风格.到目前为止,这是对这个主题最清楚的,尽管它不使用 Scala 语法,并且显式传递了延续.

Another good source, which I also point in my blog, is the Wikipedia entry on continuation passing style. That one is, by far, the most clear on the subject, though it does not use Scala syntax, and the continuation is explicitly passed.

我在博客中链接到的关于分隔延续的论文,但似乎已损坏,提供了许多用法示例.

The paper on delimited continuations, which I link to in my blog but seems to have become broken, gives many examples of usage.

但我认为分隔延续的概念的最好例子是 Scala Swarm.在其中,库在某一时刻停止代码的执行,剩余的计算成为延续.然后库会做一些事情——在这种情况下,将计算转移到另一个主机,并将结果(被访问的变量的值)返回给停止的计算.

But I think the best example of the concept of delimited continuations is Scala Swarm. In it, the library stops the execution of your code at one point, and the remaining computation becomes the continuation. The library then does something -- in this case, transferring the computation to another host, and returns the result (the value of the variable which was accessed) to the computation that was stopped.

现在,您连 Scala 页面上的简单示例都看不懂,所以请阅读我的博客.在其中,我关心解释这些基础知识,以及为什么结果是 8.

Now, you don't understand even the simple example on the Scala page, so do read my blog. In it I'm only concerned with explaining these basics, of why the result is 8.

这篇关于什么是 Scala 延续,为什么要使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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