Skip to content

Instantly share code, notes, and snippets.

@lachezar
Last active November 21, 2023 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lachezar/3ac63d88de4623216009d939864ea05b to your computer and use it in GitHub Desktop.
Save lachezar/3ac63d88de4623216009d939864ea05b to your computer and use it in GitHub Desktop.
IO implementation exercise
case class IO[A](unsafeRun: () => A):
def map[B](f: A => B): IO[B] =
IO(() => f(unsafeRun()))
def flatMap[B](f: A => IO[B]): IO[B] =
IO(() => f(unsafeRun()).unsafeRun())
val io: IO[Int] = IO(() => {
println("Running side effect!!!")
42
})
io.unsafeRun()
io.flatMap(_ => io.flatMap(_ => io)).unsafeRun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment