Last active
November 6, 2018 16:52
-
-
Save JoolsF/0d3e95bd5774c7c32678d2bb8bff26e5 to your computer and use it in GitHub Desktop.
Simple toy example of IO
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
| case class IO[A](unsafeRun: () => A) { self => | |
| def map[B](f: A => B): IO[B] = IO(() => f(self.unsafeRun())) | |
| def flatMap[B](f: A => IO[B]): IO[B] = IO(() => f(self.unsafeRun()).unsafeRun()) | |
| } | |
| def funcA(input: String): IO[Unit] = { | |
| IO(() => println(input)) | |
| } | |
| val program: IO[Unit] = for { | |
| _ <- funcA("aaa ") | |
| _ <- funcA("bbb ") | |
| _ <- funcA("ccc ") | |
| _ <- funcA("ddd ") | |
| } yield () | |
| // program only runs on calling unsafe run | |
| //program.unsafeRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment