Last active
November 21, 2023 13:20
-
-
Save lachezar/3ac63d88de4623216009d939864ea05b to your computer and use it in GitHub Desktop.
IO implementation exercise
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): | |
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