Created
October 10, 2018 15:06
-
-
Save gvolpe/3ceb00eba61a5bb3627dcd4495b234cc 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
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) |
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
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