Created
March 14, 2015 14:17
-
-
Save tkawachi/ba594b2358838ce6a484 to your computer and use it in GitHub Desktop.
ConsoleIO
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
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