Created
May 9, 2019 00:26
-
-
Save kdrakon/f16958d2d7ad5e215274bc8ea5323d18 to your computer and use it in GitHub Desktop.
Short-circuit a list of Cats Effect (IO) using a foldLeft
This file contains 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
object Main extends App { | |
val l = List(IO(println(1).asRight), IO(println(2).asRight), IO("foo".asLeft), IO(println(4).asRight)) | |
val io = l.foldLeft[IO[Either[String, Unit]]](IO(Right(())))((prev, next) => { | |
prev.flatMap({ | |
case Left(err) => IO(Left(err)) | |
case Right(_) => next | |
}) | |
}) | |
println(io.unsafeRunSync()) | |
// will print: | |
// 1 | |
// 2 | |
// Left(foo) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment