代码之家  ›  专栏  ›  技术社区  ›  Mohamed Bana

用于Scala延续

  •  17
  • Mohamed Bana  · 技术社区  · 15 年前

    在Scala中,人们如何在越来越大和越来越小的范围内使用continuation?

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

    在使用continuations时是否有重大性能损失?

    2 回复  |  直到 15 年前
        1
  •  14
  •   IttayD    15 年前

    我用它来转换窗体的异步函数 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)))
    

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

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

        2
  •  7
  •   oluies    15 年前

    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()
        }
      }
    }
    
    推荐文章