Skip to content

Instantly share code, notes, and snippets.

@salomvary
Created December 19, 2017 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salomvary/8d68a56ee9e723f766488fd8a67ab577 to your computer and use it in GitHub Desktop.
Save salomvary/8d68a56ee9e723f766488fd8a67ab577 to your computer and use it in GitHub Desktop.
Parallel Composition of Scala Futures
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Author(id: Long, name: String)
case class Publication(id: Long, authorId: Long, title:String)
case class AuthorPublications(author: Author, publications: List[Publication])
def findAuthor(query: String): Future[Long] = ???
def getAuthor(id: Long): Future[Author] = ???
def getPublications(authorId: Long): Future[List[Publication]] = ???
def findPublications(query: String): Future[AuthorPublications] =
for {
authorId <- findAuthor(query)
author = getAuthor(authorId)
pubs = getPublications(authorId)
(author, pubs) <- for {
author <- author
pubs <- pubs
} yield (author, pubs)
} yield AuthorPublications(author, pubs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment