Created
February 5, 2017 07:15
-
-
Save amitayh/62ea6f539f6a3041cf005e9d5985b19d to your computer and use it in GitHub Desktop.
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 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