问题标签 [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 回答
1137 浏览

haskell - 续传风格——函数组合

我正在使用 Racket 学习 CPS,并且我已经设法编写了这些函数:

他们似乎工作正常

我想知道这些功能是否仍然是“真正的 CPS”。我用这些函数搞砸了“真正的”延续传递吗?在 CPS 中使用函数组合技术是否符合规定?是鼓励吗?或者这样做是否会被视为“妥协”?有没有更多的 CPS-y 方式来做到这一点?

是的,我知道我刚刚问了 5 个问题,但它们背后的基本思想(我不确定我是否理解正确)是相同的。其他 Lisps、Haskell、Erlang 或其他函数式语言的解释都很好。

0 投票
1 回答
1618 浏览

serialization - 实际使用 Lua 协程/延续序列化来简化异步逻辑?

LuaPluto 库声称能够序列化 Lua 协程。我将其解释为“可序列化的延续”,这是使异步编程以同步风格可写的一个重要特征。

例如,工作流可以线性表示,而不需要命名入口点

代替

虽然将 if 语句翻译成以前的样式还不错,但一旦涉及局部变量、循环、嵌套函数调用等,事情就会变得非常复杂。

这就是可序列化的延续变得至关重要的地方。

序列化延续用于 JavaFlow、Cocoon (Rhink)、Seaside、PLT Scheme、SICS,非常适合处理业务工作流、医疗诊断和(在我的情况下)文本冒险游戏。

有没有 Lua 和 Pluto 以这种方式利用它们的功能的示例(希望是开源的!),使用延续来简化异步环境中的逻辑?

0 投票
3 回答
1295 浏览

scala - 带有 Scala 延续的事件监听器

假设我必须编写一些 GUI 代码,如下所示:

您将如何使用 Scala 延续以 CPS 风格编写它?

0 投票
7 回答
2271 浏览

recursion - 解释 The Little Schemer 的 p.137 的延续例子

有问题的代码是这样的:

我整天盯着这个,但我似乎不太明白。当您重新定义您正在重新定义的函数时,col但在示例中它们似乎使用了原始定义。为什么不改。如果不传入参数newlatseen.

很难解释我的问题,因为我似乎只是错过了一块。如果也许有人可以提供比这本书更明确的演练,我也许能够理解它是如何工作的。

0 投票
3 回答
793 浏览

haskell - Continuation Passing Style (CPS) during graph construction

I am working on a library for subdivision surfaces. In order to represent the mesh topology I'm using a kind of split-vertex lath data structure (see the diagram on the left side).

Mesh data structure

During the construction of a mesh, that also can be seen as a graph, it create nodes that should point to another ones that don't exist yet (see diagram on the right side - dashed arrow represent future links). The classical solution is to create a node with an empty pointer and then update it when the other one is created. Since I'm working on Haskell :) and I don't want to go to the dark side of the code (impurity) I'm wondering if it is possible to construct a mesh (graph) without update the data. I guess that CPS (Continuation Passing Style) could do the job but I can't figure out a way.

Is it just a dream?

UPDATE

Let me clarify my question a little bit. I am looking for a method to create nodes with direct links (pointers) and by direct link I mean no intermediate tables or maps. Just a plain data definition like that:

If I'm not wrong and if it is doable, CPS would allow an efficient creation (without node updates) and efficient transverse (without lookups on maps) of a graph. On the other hand the graph would become totally immutable i.e. a single change needs to be propagated through the whole graph e.g. changing the tail of a list.

Am I wrong? If no, how to do it?

0 投票
3 回答
344 浏览

f# - 如何在 CPS 中编写此内容?

我正在尝试掌握延续传球风格 (CPS),因此我正在修改 Gary Short 不久前向我展示的一个示例。我没有他的示例源代码,所以我试图从内存中修改他的示例。考虑以下代码:

我不太清楚的是如何构建阻力函数。我早些时候想出了这个:

但是,当然,这不是使用 CPS——更不用说它看起来真的很 hacky 并且有相当多的重复代码,这也似乎是代码的味道。

有人可以告诉我如何以 CPS 方式重写阻力函数吗?

0 投票
2 回答
5105 浏览

scheme - 为什么延续传球风格

在Kent Dybvig的 The Scheme Programming Language(第 4 版)第 3.4 节中,他非常清楚地描述了什么是延续传递风格。至于为什么,他给出了两个原因:

  1. 将多个结果传递给它的延续,因为实现延续的过程可以接受任意数量的参数。
  2. CPS 还允许一个过程采用单独的延续......,它可以接受不同数量的参数。

由于第一个原因也可以使用valuesprocedure 和第二个 using来完成case-lambda,我不清楚使用延续传递样式的优点。有人可以向我展示一些示例,说明哪些地方适合继续传递样式,哪些地方可以使代码更好、更清晰等?

0 投票
1 回答
819 浏览

scala - 延续和理解——有什么不兼容的?

我是 Scala 的新手,并试图将我的头脑围绕在我试图重现yield returnC# 语句的延续上。

这篇文章之后,我编写了以下代码:

如您所见,由于gen2无法编译而被注释掉。由于我可以使用 while 循环轻松迭代列表的内容(请参阅gen3参考资料),因此我希望 foreach 循环也能正常工作。

编译错误如下:

为什么我会收到这个错误,有没有办法用比 while 循环更干净的东西来解决这个问题?

谢谢

0 投票
2 回答
5890 浏览

scheme - 转换为 CPS(继续传递样式)

如何将 Scheme 中的这些程序转换为 CPS 形式?

  1. /li>
  2. /li>
  3. /li>

*这不是作业!

0 投票
1 回答
304 浏览

scheme - 如何以 CPS 形式编写分析器/评估器函数,如“eval-if”?

我正在尝试基于 SICP 中的元循环评估器编写一个玩具 python 方案解释​​器。由于 python 只支持有限深度的调用堆栈,我必须消除尾调用。我阅读了蹦床并用它实现了解析器。

但我不知道如何以连续传递风格编写分析器/评估器函数以将它们与蹦床一起使用。例如,eval-if函数:

在蟒蛇中:

当我想检查谓词是否为真时,我必须对其进行新一轮的调用eval。我应该如何以 CPS 形式编写这种递归调用?