Guest User

Untitled

a guest
Jul 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. // Introduce a flip method: it looks tricky, but as usual it just "follows the types":
  2. private def flip[L, R](eitherFuture: Either[L, Future[Either[L, R]]]): Future[Either[L, R]] = {
  3. eitherFuture.fold[Future[Either[L, Either[L, R]]]](
  4. left => Future.successful[Either[L, Either[L, R]]](Left(left)),
  5. right => right.map(Right(_))
  6. ).map(_.joinRight)
  7. }
  8.  
  9. // Now we can count on flapMap to do the magic!
  10. def processRequest(a: A): Future[Result] = {
  11. for {
  12. b <- f1(a)
  13. c <- flip(b.map(f2))
  14. d <- flip(c.map(f3))
  15. r <- flip(d.map(f4))
  16. } yield r.getOrElse[Result](r.left.get)
  17. }
Add Comment
Please, Sign In to add comment