Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.69 KB | None | 0 0
  1. sealed trait IO[+A] { self =>
  2.   def run: A
  3.  
  4.   def map[B](f: A => B): IO[B] =
  5.     flatMap(a => IO.unit(f(a)))
  6.  
  7.   def flatMap[B](f: A => IO[B]): IO[B] =
  8.     new IO[B] {
  9.       def run = f(self.run).run
  10.     }
  11. }
  12.  
  13. object IO {
  14.   def unit[A](a: A): IO[A] = new IO[A] { def run = a }
  15. }
  16.  
  17. case object ReadLine extends IO[String] {
  18.   def run = { readLine }
  19. }
  20.  
  21. case class WriteLine(msg: String) extends IO[Unit] {
  22.   def run = { println(msg) }
  23. }
  24.  
  25. abstract class Application {
  26.   def universe: IO[Unit]
  27.   def main(args: Array[String]) {
  28.     universe.run
  29.   }
  30. }
  31.  
  32. object Main extends Application {
  33.   def universe =
  34.     for {
  35.       l <- ReadLine
  36.       _ <- WriteLine(l)
  37.     } yield ()
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement