问题标签 [continuation-passing]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
639 浏览

scala - Scala continuations: many shifts in sequence

I have been trying to wrap my head around the complex typing issues with scala continuations. I've been reading all the material I can find on it, including the reference docs on the continuations package. I think I have it figured out to some degree and it makes SOME sense when you think about it.

I think my understanding of it (and some of my question) can be best summed up by this program:

Incidentally, it's criminal that StackOverflow doesn't highlight scala! (I stand corrected; it actually does a pretty good job but just not in the live preview)

My questions are as follows:

  1. Judging from the comments in the program above, what is missing from my understanding of scala's CPS? Is there ever a situation where you would NOT want Unit as B in @cpsParam[B,C]?
  2. The above program compiles and works (it prints out "5.0"). But, the issue I'm running into now that's causing my confusion is when I change the reset block to try to call method2 after method1:

(Apparently you can't have a code block right after a list)

When I do this (which seems like a pretty simple thing), I get the following compiler error at the reset (this is scala 2.10 Milestone 2):

I interpret this to mean "Your first shift returns a Float and your second shift returns a String and you can't do that." Is this accurate? Does that mean you cannot use CPS to do two (or more) things in sequence unless they have the same return type? Because that seems like kind of a serious limitation. I'm guessing I'm either 1) Missing something that allows you to do this, or B) Missing some obvious reason why it's impossible for that to happen with CPS. But which one is it?

I'm starting to feel less like you need to be a post-doc student in order to understand scala's CPS. But I'm certainly not quite there yet.

0 投票
1 回答
680 浏览

javascript - memoize 延续传递风格函数

我想知道是否有一种方法可以实现一个通用的“memoize”函数(如在一个函数作为输入和一个函数作为输出的函数中,作为 python 的装饰器),它也能够处理 cps 样式的函数。

对于普通函数(如“结果值通过返回返回,参数仅用于输入!”),memoize 函数可以像(在 javascript 中)一样简单

但是我的简单函数无法记住 cps 风格的memoize函数,因为我需要“再次”评估函数类型的参数,还需要知道要传递给它们的参数。

例如,给定函数

也许我可以找到它next是一个函数,但它的签名(嗯......也许,但它很棘手),绝对不是函数中使用的参数!

有人可以告诉我我错了吗?:D

我有兴趣能够记住半打 cps 风格的函数,我不想弄乱在每个函数中插入“缓存”的逻辑。

0 投票
2 回答
1061 浏览

javascript - 延续传递风格与管道有什么不同吗?

我一直在学习继续传递样式,特别是在 javascript 中实现的异步版本,其中一个函数将另一个函数作为最终参数并创建对其的异步调用,将返回值传递给第二个函数。

但是,我不太明白延续传递除了重新创建管道(如在 unix 命令行管道中)或流之外还有什么作用:

对比

除了管道要干净得多。使用管道,很明显数据被传递,同时执行被传递给接收程序。事实上,对于管道,我希望数据流能够继续通过管道,而在 CPS 中,我希望是一个串行过程。

可以想象,如果通信对象和更新方法与数据一起传递,而不是完整的切换和返回,CPS 可以扩展到连续管道。

我错过了什么吗?CPS 是否在某些重要方面有所不同(更好?)?

明确地说,我的意思是继续传递,其中一个函数将执行传递给另一个函数,而不仅仅是简单的回调。CPS 似乎暗示将一个函数的返回值传递给另一个函数,然后退出。

0 投票
4 回答
1196 浏览

recursion - The Little Schemer 仅偶数*&co

我很难理解evens-only*&co第 145 页上 The Little Schemer 的示例发生了什么。

这是代码:

初始col可以是:

我没有得到的是,使用las时,它会立即使用as和as'((1) 2 3)进入决赛。很好,但是我的大脑一片空白,试图从, ,中找出, , 。如果有人可以指导我如何设置 DrRacket 或 Chez Scheme 或 MIT-Scheme 来运行步进器,那也会很有帮助。else(car l)(1)(cdr l)(2 3)dnewldproductdsumnewlproductsum

但也许我太早了。第一次阅读这篇文章的初学者真的应该理解这种疯狂的延续吗?

0 投票
1 回答
254 浏览

scheme - 我的 CPS 是对的吗?

在“The Scheme Programming Language 4th Edition”中,有一个例子如下:

(产品'(1 2 3 4 5))=> 120

(产品'(7 3 8 0 1 9 5))=> 0

后来它在 3.3 中转换为 CPS,如下所示

(产品'(1 2 3 4 5)(λ(x)x))=> 120

(产品'(7 3 8 0 1 9 5)(λ(x)x))=> 0

我想自己做,对应的CPS如下

(产品'(1 2 3 4 5)1(λ(x)x))=> 120

(产品'(1 2 0 4 5)1(λ(x)x))=> 0

我想问一下我的CPS对吗?提前谢谢!

最好的祝福

0 投票
2 回答
226 浏览

haskell - 有没有办法在 do 表示法中捕获延续?

由于以下做块:

脱糖为以下形式:

这里不是\x -> ...而且y -> ...实际上是延续吗?

我想知道是否有一种方法可以捕获 定义中的延续bind,但我无法获得正确的类型。IE:

现在我试着混淆了这些类型:

但这也不起作用。有没有办法捕捉这些隐含的延续?

顺便说一句,我知道Cont单子,我只是在试验和尝试一些东西。

0 投票
1 回答
583 浏览

list - 将列表转换为元组

我有几个命令行选项(例如 5 个),我想将它们转换为元组。问题是我希望它们以正确的顺序出现,因此可以使用模式匹配从列表轻松构建元组,但在现实生活中可以随机顺序提供选项,所以我不知道列表的头部是否包含详细选项或日志文件名?

我试图思考如何使用 continuation-passing 风格来做到这一点,但是我没有想到任何有用的东西。

那有可能吗?

我认为我可以“排序”列表以使其按预测顺序排列,但看起来并不好。

我也可以摆脱元组并创建数据记录——但这仍然会导致检查属性类型并设置记录的正确字段。打字还是很多的。

0 投票
2 回答
1434 浏览

ocaml - 为什么 OCaml 标准库有这么多非尾递归函数?

我最近一直在重写许多 OCaml 标准库函数,使其成为尾递归的。鉴于这需要直接的 CPS 转换,我对为什么不以这种方式编写默认版本感到困惑。

例如,在标准库中,map 定义为:

我已将其重写为:

0 投票
2 回答
555 浏览

javascript - JavaScript:直接代码与 CPS 样式生成的代码性能比较

在我的应用程序中,我正在生成遵循 CPS 样式的 JavaScript 代码。我“不”使用任何“延续”。没有异步行为,没有暂停和恢复,没有回调。

只是代码遵循了一种延续传递的编程风格。

功能有许多阶段,每个阶段都进行处理并将结果传递给其延续。

我发现 CPS 样式代码的性能很差。以直接样式编写的代码几乎比 CPS 样式代码快 150 倍。

请检查以下代码。
下面的两个代码都等价于

直接样式代码:

上面的代码运行了将近 95 毫秒。

CPS 样式代码:

上述 CPS 风格的代码运行时间为 15000 毫秒

我能做些什么来改进 CPS 风格的代码吗?或者 JavaScript 天生不适合 CPS 样式的代码?

以上测试是在node.js 0.6.12版本上完成的

有人可以对这个问题有所了解吗?

谢谢,

0 投票
1 回答
1106 浏览

haskell - 继续传递类​​型的样式表示