Scala:分隔延续解释 - 不

发布于 2024-12-26 16:05:58 字数 730 浏览 5 评论 0原文

对延续的概念很感兴趣,我开始阅读 wiki、帖子,并看到这个“简单”的例子:

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

如果不了解 Scala,我完全迷失了方向,无法弄清楚 8 是如何产生的。

以下是我试图弄清楚其含义但失败的方法。有谁能给我一个简短的解释吗?是的,有 Scala 语法书,但它们太厚了,我对理解定界延续概念比掌握 Scala 语言更感兴趣......

  • (Int=>Int)

    看起来像一个 C# 委托,输入是 Int,输出是 Int。

  • <代码>k: (Int=>Int) => k(7)

    我在这里迷路了...... k 是什么,(Int=>Int)=>k(7) 是什么?

  • shift { k: (Int=>Int) =>; k(7) } + 1

    更多的迷失...

  • 重置 { ...;移位 { k: (Int=>Int) => k(7) } + 1 }

    甚至越来越迷失...

Interested in the concept of continuation, I started reading wikis, posts, and came to this "simple" example:

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

Without knowledge of Scala, I'm totally lost here, could not figure out how the 8 comes out.

Below is how I tried to figure out the meaning but failed. Any guy could you please give me a short explanation? Yeah there are Scala grammar books but they are too thick, I'm more interested in understanding delimited continuation concept than master Scala language...

  • (Int=>Int)

    Looks like a C# delegate, input is Int, output is Int.

  • k: (Int=>Int) => k(7)

    I'm lost here... what is k, and what is (Int=>Int)=>k(7)?

  • shift { k: (Int=>Int) => k(7) } + 1

    Even more lost...

  • reset { ...; shift { k: (Int=>Int) => k(7) } + 1 }

    Even even more and more lost...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

残月升风 2025-01-02 16:05:58

我找到了 Chris League 的演讲“Monadologie:类型焦虑的专业帮助” (http://vimeo.com/13304075 ) 包含分隔延续的更好示例之一。

I found Chris League's talk, "Monadologie: Professional Help for Type Anxiety" (http://vimeo.com/13304075) contains one of better examples of a delimited continuation.

江心雾 2025-01-02 16:05:58

内尔顿,非常感谢! Alex Neth 在链接中的回答帮助了我。现在我想我明白了。让我回答我自己的问题,作为注释。

Reset和Shift的语法:

reset {
  ...
  shift { cf: (InputParameterType => OutpututParameterType) =>
     CodeBlockInsideShift
  } 
  CodeBlockAfterShiftBeforeEndOfReset
}

在C#风格的伪代码中,它实际上意味着:

public delegate OutpututParameterType CFDelegate(InputParameterType);

CFDelegate cf = CodeBlockAfterShiftBeforeEndOfReset;

CodeBlockInsideShift;

fotNelton, thanks a lot! Alex Neth's answer in the link helped me out. Now I think I got it. Let me answer my own question to dot down as a note.

Grammar of reset and shift:

reset {
  ...
  shift { cf: (InputParameterType => OutpututParameterType) =>
     CodeBlockInsideShift
  } 
  CodeBlockAfterShiftBeforeEndOfReset
}

It actually means, in C# style pseudo code:

public delegate OutpututParameterType CFDelegate(InputParameterType);

CFDelegate cf = CodeBlockAfterShiftBeforeEndOfReset;

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