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

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

问题描述

我刚完成 在Scala中编程 ,一直在研究Scala 2.7和2.8之间的变化.似乎最重要的一个是continuations插件,但我不了解它的作用或工作方式.我已经看到它对异步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:

还有关于堆栈溢出的问题:

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.

推荐答案

我的

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

另一个好消息源,也是我在博客中指出的,是继续传递样式上的Wikipedia条目.到目前为止,尽管它没有使用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.

但是我认为定界延续的 concept 的最好例子是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页面上的简单示例,所以 do 阅读我的博客.在其中,我负责解释这些基础知识,说明结果为何为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天全站免登陆