Created
November 16, 2012 09:19
-
-
Save wangzaixiang/4085790 to your computer and use it in GitHub Desktop.
Scala CPS learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import scala.util.continuations._ | |
object Demo1 { | |
def main(args: Array[String]) { | |
var cont: (Int=>String) = null | |
println("before reset") | |
reset { | |
println("before shift") | |
val result = shift { k: (Int=>String) => | |
cont = k | |
println("inside shift") | |
} | |
println(s"after shift result = $result") | |
result.toString | |
} | |
println("after reset") | |
println(cont(5)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import scala.util.continuations._ | |
object Demo1U { | |
def main(args: Array[String]) { | |
var cont: (Int => String) = null | |
def resetBody(): ControlContext[Int, String, Unit] = { | |
println("before shift") | |
shiftR(fShift _).map(fResume _) | |
} | |
def fShift(k: Int => String) { | |
cont = k | |
println("inside shift") | |
} | |
def fResume(result: Int): Int = { | |
println(s"after shift result = $result"); | |
result | |
} | |
println("before reset") | |
val pkg = scala.util.continuations.`package` | |
val method = pkg.getClass().getMethod("reset", classOf[Function0[_]]) | |
method.invoke(pkg, resetBody _) | |
println("after reset") | |
println(cont(5)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import scala.util.continuations._ | |
object Demo2{ | |
def main(args: Array[String]) { | |
println("before reset") | |
var cont: (Int=>String) = null | |
var str: String = "" | |
reset { | |
var i = 0 | |
while(i < 2) { | |
val hole: Int = shift { k:(Int=>String) => | |
cont = k | |
println(s"inside shift loop = $i") | |
s"continue at loop = $i" | |
} | |
cont = null | |
str = str + hole | |
println(s"reset: loop=$i hole = $hole str = $str") | |
i += 1 | |
} | |
str | |
} | |
var i = 0 | |
while(cont != null) { | |
i += 1 | |
println(s"cont($i) = ${cont(i)}"); | |
println(s"main: str=$str") | |
} | |
println(s"str = $str") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import scala.util.continuations._ | |
object Demo2U { | |
def main(args: Array[String]) { | |
println("before reset") | |
var cont: (Int => String) = null | |
var str = "" | |
object ResetBody extends Function0[ControlContext[String, String, String]] { | |
var i = 0 | |
def apply() = { | |
i = 0 | |
whileBody(i).map( _=>str) | |
} | |
def whileBody(i: Int): ControlContext[Unit, String, String] = { | |
if (i < 2) { | |
val cc = shiftR(fShift _).map(fResume _) | |
cc.flatMap(_ => whileBody(i)) | |
} else | |
shiftUnitR(()) | |
} | |
def fShift(f: Int => String): String = { | |
cont = f | |
println(s"inside shift loop = $i") | |
s"continue at loop = $i" | |
} | |
def fResume(hole: Int): Unit = { | |
cont = null | |
str = str + hole | |
println(s"reset: loop=$i hole = $hole str = $str") | |
i += 1 | |
} | |
} | |
val pkg = scala.util.continuations.`package` | |
val method = pkg.getClass().getMethod("reset", classOf[Function0[_]]) | |
method.invoke(pkg, ResetBody) | |
var i = 0 | |
while (cont != null) { | |
i += 1 | |
println(s"cont($i) = ${cont(i)}"); | |
println(s"main: str=$str") | |
} | |
println(s"str = $str") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment