Skip to content

Instantly share code, notes, and snippets.

@tzachz
Last active July 20, 2018 20:19
Show Gist options
  • Save tzachz/2bff1a01d499e1ce67fb4bde695f2bec to your computer and use it in GitHub Desktop.
Save tzachz/2bff1a01d499e1ce67fb4bde695f2bec to your computer and use it in GitHub Desktop.
CompositionPost1
sealed trait Result
class Success extends Result
class Failed extends Result
// For some types A, B, C, D, we have these async computations which may fail:
def f1(a: A): Future[Either[Failed, B]] = ???
def f2(b: B): Future[Either[Failed, C]] = ???
def f3(c: C): Future[Either[Failed, D]] = ???
def f4(d: D): Future[Either[Failed, Result]] = ???
// We want to apply each function to the result of the previous one, so that:
// - If one returns a Failed result, return that failure without applying the next function
// - If a Future fails, return that failed future without applying the next function
// This obviously won't compile - but that's what we want conceptually:
def processRequest(a: A): Future[Result] = f4(f3(f2(f1(a))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment