Created
July 20, 2018 20:02
-
-
Save tzachz/b8f6475e84f549d3793a04a4cdb5fcb1 to your computer and use it in GitHub Desktop.
CompositionPost2
This file contains hidden or 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
| // we'll start by applying f1 and mapping the future result by pattern-matching Left or Right: | |
| def processRequest(a: A): Future[Result] = f1(a).flatMap { | |
| case Right(b) => getC(b) | |
| case Left(f) => Future.successful(f) | |
| } | |
| // Now we do something similar to the result, this time applying f2: | |
| private def getC(b: B): Future[Result] = f2(b).flatMap { | |
| case Right(c) => getD(c) | |
| case Left(f) => Future.successful(f) | |
| } | |
| // and again, for f3: | |
| private def getD(c: C): Future[Result] = f3(c).flatMap { | |
| case Right(d) => getResult(d) | |
| case Left(f) => Future.successful(f) | |
| } | |
| // lastly, applying f4 and mappting the returned Either into a Result: | |
| private def getResult(d: D): Future[Result] = | |
| f4(d).map(either => either.getOrElse(either.left.get)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment