-
-
Save prassee/8280153 to your computer and use it in GitHub Desktop.
CPS Exmaple Code in Scala
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
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