Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Created March 14, 2015 14:17
Show Gist options
  • Save tkawachi/ba594b2358838ce6a484 to your computer and use it in GitHub Desktop.
Save tkawachi/ba594b2358838ce6a484 to your computer and use it in GitHub Desktop.
ConsoleIO
import scalaz.Free.FreeC
import scalaz.{Free, ~>}
import scalaz.std.function._
sealed trait Console[A]
case object ReadLine extends Console[Option[String]]
case class PrintLn(s: String) extends Console[Unit]
object Console {
type ConsoleIO[A] = FreeC[Console, A]
def readLine: ConsoleIO[Option[String]] = Free.liftFC(ReadLine)
def printLn(s: String): ConsoleIO[Unit] = Free.liftFC(PrintLn(s))
val interpreterF0 = new (Console ~> Function0) {
override def apply[A](fa: Console[A]): () => A = fa match {
case ReadLine => () => Option(scala.io.StdIn.readLine())
case PrintLn(s) => () => println(s)
}
}
}
object ConsoleMain {
import Console._
def main(args: Array[String]): Unit = {
val program = for {
_ <- printLn("Hello")
x <- readLine
} yield x
val fun = Free.runFC(program)(interpreterF0)
fun()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment