Skip to content

Instantly share code, notes, and snippets.

@gvolpe
Created October 10, 2018 15:06
Show Gist options
  • Save gvolpe/3ceb00eba61a5bb3627dcd4495b234cc to your computer and use it in GitHub Desktop.
Save gvolpe/3ceb00eba61a5bb3627dcd4495b234cc to your computer and use it in GitHub Desktop.
def genericgeneric[[FF[_]: [_]: MonadErrorMonadError[?[_], [?[_], ThrowableThrowable], ], GG[_]: [_]: TraverseTraverse, , AA](
gfa: ]( gfa: GG[[FF[[AA]],
append: ]], append: GG[[AA] => ] => AA => => GG[[AA],
ref: ], ref: RefRef[[FF, , GG[[AA]]
): ]] ): FF[[GG[[AA]] =
gfa
.traverse(_.attempt.flatTap {
]] = gfa .traverse(_.attempt.flatTap { casecase RightRight(x) => ref.update(append(_)(x))
(x) => ref.update(append(_)(x)) casecase LeftLeft(_) => (_) => ApplicativeApplicative[[FF].unit
}.rethrow)
.handleErrorWith(_ => ref.get)].unit }.rethrow) .handleErrorWith(_ => ref.get)
generic[IO, List, String](list2, g => x => g :+ x, ref)
val ioa = IO.sleep(1.second) *> IO.pure("a")
val iob = IO.pure("b")
val ioc = IO.pure("c")
val iod = IO.sleep(3.seconds) *> IO.pure("d")
val ioe = IO.sleep(2.seconds) *> IO.pure("e")
val list1 = List(ioa, iob, ioc, iod, ioe)
val list2 = List(ioa, iob, IO.raiseError(new Exception("boom")), iod, ioe)
def collectSuccessful(list: List[IO[String]])(ref: Ref[IO, List[String]]): IO[List[String]] =
list
.traverse(_.attempt.flatTap {
case Right(x) => ref.update(_ :+ x)
case Left(_) => IO.unit
}.rethrow)
.handleErrorWith(_ => ref.get)
val program = Ref.of[IO, List[String]](List.empty).flatMap { ref =>
collectSuccessful(list2)(ref).flatMap(x => IO(println(x)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment