Skip to content

Instantly share code, notes, and snippets.

@virasak
Created August 20, 2009 04:41
Show Gist options
  • Save virasak/170839 to your computer and use it in GitHub Desktop.
Save virasak/170839 to your computer and use it in GitHub Desktop.
object Cli {
sealed abstract class EvalResult
object Exit extends EvalResult
case class ReadMore(lines: List[String]) extends EvalResult
case class PrintMe(result: (String => Unit) => Unit) extends EvalResult
def prompt(ps1: String, ps2: String)(eval: List[String] => EvalResult): Unit = {
def run(ps: String, acc: List[String]): Unit = eval(readLine(ps) :: acc) match {
case Exit => ()
case ReadMore(lines) => run(ps2, lines)
case PrintMe(result) => result(print _); run(ps1, Nil)
}
run(ps1, Nil)
}
}
object More {
def unapply(x: String): Option[String] =
if (x.endsWith("\\"))
Some(x.substring(0, x.length - 1))
else
None
}
Cli.prompt("Enter a sentence: ", ">>> ") {
case "quit" :: Nil => Cli.Exit
case More(x) :: xs => Cli.ReadMore( x :: xs)
case xs => Cli.PrintMe { callback =>
callback("Your input is so long. Please wait...\n")
Thread.sleep(10000)
callback("Ok. After hard work. ")
callback {
"That string contains %d vowels.\n".format {
xs.foldLeft(0) {
(acc, x) => acc + "(?i)[aeiou]".r.findAllIn(x).collect.size
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment