“呼叫抄送” Scala 中的模式?

发布于 2024-11-07 21:43:55 字数 241 浏览 6 评论 0原文

我找到了一篇很好的文章,关于使用当前延续进行调用 图案。据我了解,他们使用Scheme和无限延续。文章中的模式可以在 Scala 中实现吗?有没有关于 Scala 中的分隔延续模式的文章?

I found a good article, about call with current continuation patterns. As I understand, they use Scheme and undelimited continuations. Can the patterns from the article be implemented in Scala? Is there any article about delimited continuations patterns in Scala ?

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

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

发布评论

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

评论(2

稀香 2024-11-14 21:43:55

是的,他们绝对可以。 callCC 在 Scala 中看起来像这样:

def callCC[R, A, B](f: (A => Cont[R, B]) => Cont[R, A]): Cont[R, A] =
  Cont(k => f(a => Cont(_ => k(a))) run k)

其中 Cont 是一个捕获延续的数据结构:

case class Cont[R, A](run: (A => R) => R) {
  def flatMap[B](f: A => Cont[R, B]): Cont[R, B] =
    Cont(k => run(a => f(a) run k))
  def map[B](f: A => B): Cont[R, B] =
    Cont(k => run(a => k(f(a))))
}

以下是如何使用它来模拟已检查异常:

def divExcpt[R](x: Int, y: Int, h: String => Cont[R, Int]): Cont[R, Int] =
  callCC[R, Int, String](ok => for {
    err <- callCC[R, String, Unit](notOK => for {
             _ <- if (y == 0) notOK("Denominator 0") else Cont[R, Unit](_(()))
             r <- ok(x / y)
           } yield r)
    r <- h(err)
  } yield r)

您可以按如下方式调用此函数:

scala> divExcpt(10, 2, error) run println   
5

scala> divExcpt(10, 0, error) run println
java.lang.RuntimeException: Denominator 0

Yes, they absolutely can. callCC looks like this in Scala:

def callCC[R, A, B](f: (A => Cont[R, B]) => Cont[R, A]): Cont[R, A] =
  Cont(k => f(a => Cont(_ => k(a))) run k)

Where Cont is a data structure that captures a continuation:

case class Cont[R, A](run: (A => R) => R) {
  def flatMap[B](f: A => Cont[R, B]): Cont[R, B] =
    Cont(k => run(a => f(a) run k))
  def map[B](f: A => B): Cont[R, B] =
    Cont(k => run(a => k(f(a))))
}

Here's how you might use it to simulate checked exceptions:

def divExcpt[R](x: Int, y: Int, h: String => Cont[R, Int]): Cont[R, Int] =
  callCC[R, Int, String](ok => for {
    err <- callCC[R, String, Unit](notOK => for {
             _ <- if (y == 0) notOK("Denominator 0") else Cont[R, Unit](_(()))
             r <- ok(x / y)
           } yield r)
    r <- h(err)
  } yield r)

You would call this function as follows:

scala> divExcpt(10, 2, error) run println   
5

scala> divExcpt(10, 0, error) run println
java.lang.RuntimeException: Denominator 0
谈情不如逗狗 2024-11-14 21:43:55

Scala 有一个类型化分隔延续的实现,它过去随编译器和标准库一起提供,但已被提取到 外部模块从那时起就几乎烂掉了。这是一个巨大的耻辱,我鼓励任何对定界延续感兴趣的人通过使用和贡献它来表明他们关心它的存在。

Scala has an implementation of typed delimited continuations which used to be shipped with the compiler and standard library, but has been extracted to an external module and pretty much left to rot since then. It's a great shame, and I encourage anyone who's interested in delimited continuations to show that they care about its existence by using and contributing to it.

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