Skip to content

Instantly share code, notes, and snippets.

@amitayh
Created February 5, 2017 07:15
Show Gist options
  • Save amitayh/62ea6f539f6a3041cf005e9d5985b19d to your computer and use it in GitHub Desktop.
Save amitayh/62ea6f539f6a3041cf005e9d5985b19d to your computer and use it in GitHub Desktop.
import scala.io.StdIn.readLine
trait IO[+A] {
def run(): A
def flatMap[B](f: A => IO[B]): IO[B] = IO(f(run()).run())
def map[B](f: A => B): IO[B] = flatMap(a => IO(f(a)))
}
object IO {
def apply[A](a: => A): IO[A] = new IO[A] {
def run(): A = a
}
}
def PrintLn(x: String): IO[Unit] = IO(println(x))
def ReadLn: IO[String] = IO(readLine())
val prog = for {
name <- ReadLn
_ <- PrintLn(s"Hello, $name!")
} yield ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment