Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2010 13:01
Show Gist options
  • Save anonymous/586735 to your computer and use it in GitHub Desktop.
Save anonymous/586735 to your computer and use it in GitHub Desktop.
import scala.util.continuations.{reset,shift,cpsParam}
def saveCont[A,B](resetFun : (Unit => A @cpsParam[B,Unit] ) => B @cpsParam[B,Unit]) : A => B = {
var mutCC : A => B = null;
val switchFun : (Unit => A @cpsParam[B,Unit] )= Unit => {
shift{ k : (A => B) =>
mutCC = k
}
}
reset{
resetFun(switchFun)
}
val cc = mutCC
cc
}
val cc = saveCont[Int,Int]{switch =>
val x = 5 + switch() // switch выкидывает из стека, сохраняя его в cc
x + 6
}
println(cc(5)) // cc(5) продолжает выполнять стек, будто switch() вернуло 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment