Skip to content

Instantly share code, notes, and snippets.

@gvolpe
Last active August 15, 2018 10:05
Show Gist options
  • Save gvolpe/49544f802355ee0398afd4708a2dd32d to your computer and use it in GitHub Desktop.
Save gvolpe/49544f802355ee0398afd4708a2dd32d to your computer and use it in GitHub Desktop.
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