Scala 延续的用途

发布于 2024-09-29 06:24:41 字数 95 浏览 4 评论 0原文

人们如何在 Scala 中使用更大或更小规模的延续?

Scala 标准库的任何部分是用 CPS 编写的吗?

使用延续是否有任何重大的性能损失?

How are people using continuations on a larger and smaller scale in Scala?

Are any parts of the Scala standard library written in CPS?

Are there any major performance penalties in using continuations?

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

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

发布评论

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

评论(2

拥抱没勇气 2024-10-06 06:24:41

我使用它来转换 def func(...)(followup: Result => Unit): Unit 形式的异步函数,这样

foo(args){result1 => 
  bar(result1){result2 => 
     car(result2) {result3 =>
       //etc.
     }
  }
}

您就可以编写

val result1 = foo(args)
val result2 = bar(result1)
val result3 = car(result2)

or

car(bar(foo(args)))

(注意:函数不限于一个参数或仅使用先前的结果作为参数)

请参阅 http: //www.tikalk.com/java/blog/asynchronous-functions-actors-and-cps

I'm using this to turn asynchronous functions of the form def func(...)(followup: Result => Unit): Unit so that instead of writing

foo(args){result1 => 
  bar(result1){result2 => 
     car(result2) {result3 =>
       //etc.
     }
  }
}

you can write

val result1 = foo(args)
val result2 = bar(result1)
val result3 = car(result2)

or

car(bar(foo(args)))

(note: the functions are not limited to one argument or just the use of previous results as arguments)

See http://www.tikalk.com/java/blog/asynchronous-functions-actors-and-cps

海未深 2024-10-06 06:24:41

Scala-ARM(自动资源管理)使用分隔延续

import java.io._
import util.continuations._
import resource._
def each_line_from(r : BufferedReader) : String @suspendable =
  shift { k =>
    var line = r.readLine
    while(line != null) {
      k(line)
      line = r.readLine
    }
  }
reset {
  val server = managed(new ServerSocket(8007)) !
  while(true) {
    // This reset is not needed, however the  below denotes a "flow" of execution that can be deferred.
    // One can envision an asynchronous execuction model that would support the exact same semantics as below.
    reset {
      val connection = managed(server.accept) !
      val output = managed(connection.getOutputStream) !
      val input = managed(connection.getInputStream) !
      val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)))
      val reader = new BufferedReader(new InputStreamReader(input))
      writer.println(each_line_from(reader))
      writer.flush()
    }
  }
}

Scala-ARM (Automatic-Resource-Management) uses delimited continuations

import java.io._
import util.continuations._
import resource._
def each_line_from(r : BufferedReader) : String @suspendable =
  shift { k =>
    var line = r.readLine
    while(line != null) {
      k(line)
      line = r.readLine
    }
  }
reset {
  val server = managed(new ServerSocket(8007)) !
  while(true) {
    // This reset is not needed, however the  below denotes a "flow" of execution that can be deferred.
    // One can envision an asynchronous execuction model that would support the exact same semantics as below.
    reset {
      val connection = managed(server.accept) !
      val output = managed(connection.getOutputStream) !
      val input = managed(connection.getInputStream) !
      val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)))
      val reader = new BufferedReader(new InputStreamReader(input))
      writer.println(each_line_from(reader))
      writer.flush()
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文