[Assorted-commits] SF.net SVN: assorted:[1561] sandbox/trunk/src/scala/Continuations.scala
Brought to you by:
yangzhang
From: <yan...@us...> - 2010-02-04 18:57:18
|
Revision: 1561 http://assorted.svn.sourceforge.net/assorted/?rev=1561&view=rev Author: yangzhang Date: 2010-02-04 18:57:03 +0000 (Thu, 04 Feb 2010) Log Message: ----------- more playing with continuations Modified Paths: -------------- sandbox/trunk/src/scala/Continuations.scala Modified: sandbox/trunk/src/scala/Continuations.scala =================================================================== --- sandbox/trunk/src/scala/Continuations.scala 2010-02-04 18:49:25 UTC (rev 1560) +++ sandbox/trunk/src/scala/Continuations.scala 2010-02-04 18:57:03 UTC (rev 1561) @@ -1,11 +1,100 @@ import scala.continuations.ControlContext._ +/* +class YieldReturnIterator[+T] extends Iterator[T] { + private[this] var nextValue: Option[Option[T]] = None + private[this] var getNext: () => Unit = null + protected def yieldReturn(x: T): Unit @cps[Unit, Unit] = { + shift { cont => + nextValue = Some(Some(x)) + getNext = cont + } + } + protected def body: Unit @cps[Unit, Unit] { + getNext = { () => + reset { + body + nextValue = Some(None) + getNext = null + } + } + } + def hasNext: Boolean = { + if (nextValue.isEmpty) + getNext() + nextValue.get.isDefined + } + def next: String = { + if (nextValue.isEmpty) + getNext() + val e = nextValue.get.get + nextValue = None + e + } +} + +class My(array: Array[String]) extends YieldReturnIterator[String] { + def body = for (s <- array) yieldReturn(s) +} +*/ + object Continuations { def main(args: Array[String]) { reset { - println("first") - shift { k: (Unit => Unit) => k(k(())) } - println("last") + println("outside pre") + val shi = shift { k: (Unit => Unit) => + println("inside pre") + k() + println("inside mid") + k() + println("inside post") + } + println("outside post") + } // reset always just returns Unit + + println + + reset { + println("outside pre") + val a = shift { k: (Int => Double) => + println("inside pre") + val b = k(2) // think of there being two cothreads; this thread msg-passes a 2 + println("inside mid " + b) + val c = k(4) // pass a 4 + println("inside post " + c) + } + println("outside post " + a) + 6.0 + } // reset always just returns Unit + + println + + def next = shift { k: (Int => Unit) => + k(1) + k(2) + k(3) } + + reset { println(next) } // prints 1 2 3 + reset { println(next) } // prints 1 2 3 + + println + + //var nextVal: Int = 0 + //var getNext: () => Int = null + //def next2 = shift { k: (Unit => Unit) => + // getNext = { () => + // nextVal += 1 + // k() + // nextVal + // } + //} + + //reset { + // next2 + // println(getNext) + // next2 + // println(getNext) + //} } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |