Last active
August 15, 2018 10:05
-
-
Save gvolpe/49544f802355ee0398afd4708a2dd32d to your computer and use it in GitHub Desktop.
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
import cats.effect._ | |
import cats.syntax.apply._ | |
import fs2._ | |
import scala.util.Random | |
val ioa: IO[Option[Int]] = IO(Random.nextInt(100)).flatMap { n => | |
if (n % 2 == 0) IO(println(s"$n")) *> IO.pure(Some(n)) | |
else IO(println(s"ERROR >> $n")) *> IO.raiseError(new Exception(s"Error $n")) | |
} | |
val stream = | |
Stream | |
.repeatEval(ioa.attempt) | |
.take(10) | |
.mapAccumulate(List.empty[String]) { | |
case (acc, Left(t)) => (t.getMessage :: acc, None: Option[Int]) | |
case (acc, Right(x)) => (acc, x) | |
} | |
println(stream.compile.last.unsafeRunSync) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment