代码之家  ›  专栏  ›  技术社区  ›  xyzzyrz

使用Scala continuations实现收益率(收益率-回报率)

  •  27
  • xyzzyrz  · 技术社区  · 16 年前

    如何实现C# yield return 使用Scala连续体?我希望能够编写Scala Iterator this Scala news post ,但它不起作用(尝试使用Scala 2.8.0测试版)。答案是 related question

    2 回复  |  直到 9 年前
        1
  •  41
  •   Rich Dougherty    16 年前

    在引入continuations之前,我们需要构建一些基础设施。 trampoline 这在 Iteration 物体。 Yield Done .

    sealed trait Iteration[+R]
    case class Yield[+R](result: R, next: () => Iteration[R]) extends Iteration[R]
    case object Done extends Iteration[Nothing]
    
    def trampoline[R](body: => Iteration[R]): Iterator[R] = {
      def loop(thunk: () => Iteration[R]): Stream[R] = {
        thunk.apply match {
          case Yield(result, next) => Stream.cons(result, loop(next))
          case Done => Stream.empty
        }
      }
      loop(() => body).iterator
    }
    

    Stream . Iterator 打电话 iterator 通过使用 我们的评价是懒惰的;在需要下一次迭代之前,我们不会对其进行评估。

    蹦床可以直接用来建造迭代器。

    val itr1 = trampoline {
      Yield(1, () => Yield(2, () => Yield(3, () => Done)))
    }
    
    for (i <- itr1) { println(i) }
    

    这写起来非常糟糕,所以让我们使用分隔的连续体来创建 迭代

    shift 和 reset 运算符将计算分解为 S trampoline 迭代 它变成了一个 迭代器 .

    import scala.continuations._
    import scala.continuations.ControlContext.{shift,reset}
    
    def iterator[R](body: => Unit @cps[Iteration[R],Iteration[R]]): Iterator[R] =
      trampoline {
        reset[Iteration[R],Iteration[R]] { body ; Done }
      }
    
    def yld[R](result: R): Unit @cps[Iteration[R],Iteration[R]] =
      shift((k: Unit => Iteration[R]) => Yield(result, () => k(())))
    

    val itr2 = iterator[Int] {
      yld(1)
      yld(2)
      yld(3)
    }
    
    for (i <- itr2) { println(i) }
    

    好多了!

    C# reference page 对于 yield 这显示了一些更高级的用法。

    def power(number: Int, exponent: Int): Iterator[Int] = iterator[Int] {
      def loop(result: Int, counter: Int): Unit @cps[Iteration[Int],Iteration[Int]] = {
        if (counter < exponent) {
          yld(result)
          loop(result * number, counter + 1)
        }
      }
      loop(number, 0)
    }
    
    for (i <- power(2, 8)) { println(i) }
    
        2
  •  5
  •   Community Mohan Dere    9 年前

    在多玩了几个小时后,我终于找到了一种方法。我认为这比我迄今为止看到的所有其他解决方案都要简单,尽管后来我非常欣赏Rich和 Miles' 解决。

    def loopWhile(cond: =>Boolean)(body: =>(Unit @suspendable)): Unit @suspendable = {
      if (cond) {
        body
        loopWhile(cond)(body)
      }
    }
    
      class Gen {
        var prodCont: Unit => Unit = { x: Unit => prod }
        var nextVal = 0
        def yld(i: Int) = shift { k: (Unit => Unit) => nextVal = i; prodCont = k }
        def next = { prodCont(); nextVal }
        def prod = {
          reset {
            // following is generator logic; can be refactored out generically
            var i = 0
            i += 1
            yld(i)
            i += 1
            yld(i)
            // scala continuations plugin can't handle while loops, so need own construct
            loopWhile (true) {
              i += 1
              yld(i)
            }
          }
        }
      }
      val it = new Gen
      println(it.next)
      println(it.next)
      println(it.next)
    
    推荐文章