Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Last active November 6, 2018 16:52
Show Gist options
  • Select an option

  • Save JoolsF/0d3e95bd5774c7c32678d2bb8bff26e5 to your computer and use it in GitHub Desktop.

Select an option

Save JoolsF/0d3e95bd5774c7c32678d2bb8bff26e5 to your computer and use it in GitHub Desktop.
Simple toy example of IO
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