Skip to content

Instantly share code, notes, and snippets.

@prassee
Created January 6, 2014 09:15
Show Gist options
  • Save prassee/8280153 to your computer and use it in GitHub Desktop.
Save prassee/8280153 to your computer and use it in GitHub Desktop.
CPS Exmaple Code in Scala
object ReadFileCPS {
var fileName: String = "a.txt"
var cont: (Unit => Unit) = null
def capture = {
shift { k: (Unit => Unit) => cont = k
}
}
def readFailSafe {
var contents: String = ""
reset {
try {
contents = Source.fromFile(fileName).mkString
} catch { case _: Throwable => }
capture
readFailSafe
println("asafsdfas")
println(contents)
}
if (contents == "") {
println(s"the file ${fileName} doesn't exist" +
"please try another file name")
fileName = readLine()
println("new file name " + fileName)
cont()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment