Skip to content

Instantly share code, notes, and snippets.

@dschobel
Created February 15, 2013 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dschobel/4957600 to your computer and use it in GitHub Desktop.
Save dschobel/4957600 to your computer and use it in GitHub Desktop.
gathering Scala futures
def gather_futures[A](xs: Seq[Future[A]]): Future[Seq[A]]= {
def combine[A,B,C](f1: Future[A], f2: Future[B])(f: (A,B) => C): Future[C] ={
for(a <- f1; b <- f2)
yield f(a,b)
}
xs.foldLeft(Future{Seq[A]()}){(acc: Future[Seq[A]],x: Future[A]) => combine(x,acc)((a: A,b: Seq[A]) => b ++ Seq(a))}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment