人々はScalaで大小の規模で継続をどのように使用していますか?
Scala標準ライブラリの一部はCPSで書かれていますか?
継続を使用することで、パフォーマンスに大きなペナルティはありますか?
人々はScalaで大小の規模で継続をどのように使用していますか?
Scala標準ライブラリの一部はCPSで書かれていますか?
継続を使用することで、パフォーマンスに大きなペナルティはありますか?
私はこれを使ってフォームの非同期関数を回している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)
また
car(bar(foo(args)))
(注:関数は1つの引数に限定されないか、引数として以前の結果を使用するだけです)
http://www.tikalk.com/java/blog/asynchronous-functions-actors-and-cpsを参照してください
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()
}
}
}