Skip to content

Instantly share code, notes, and snippets.

@akkomar
Last active August 17, 2020 10:56
Show Gist options
  • Save akkomar/04945b5f0384717917f8 to your computer and use it in GitHub Desktop.
Save akkomar/04945b5f0384717917f8 to your computer and use it in GitHub Desktop.
Merging sequence of Futures of Lists into one Future containing merged list
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
val futures: List[Future[List[Int]]] =
List.fill(10)(Future {
Thread.sleep(100)
List(1, 2, 3)
})
// def merge[T](first: Future[Seq[T]], second: Future[Seq[T]]): Future[Seq[T]] =
// for {
// a <- first
// b <- second
// } yield a ++ b
// val future: Future[Seq[Int]] = futures.foldLeft(Future(List[Int]()):Future[Seq[Int]])(
// (acc, fut) => merge(acc, fut))
// even better:
val future: Future[List[Int]] = Future.fold(futures)(List[Int]())((acc, e) => acc ++ e)
Await.result(future, 1 second)
@filippovitale
Copy link

Nice. The "even better" could be reduce even to: Future.fold(futures)(List.empty[Int])(_ ++ _)

@umnick84
Copy link

Cool! But this works until futures doesn't contain failed future in the list. Then the final result will be an exception.

@JamesMcCreary
Copy link

This worked flawlessly for my problem. I kept trying to force a labyrinth of maps, but the fold is an interesting new function I have not used. Kudos!

@giftig
Copy link

giftig commented Apr 5, 2018

Future.sequence(futures) map { _.flatten }

@kalevivek11
Copy link

what if one of the future got blocked and never returned anything, how to handle this scenario?

@devendra-kr
Copy link

what if one of the future got blocked and never returned anything, how to handle this scenario?
=> use promise is this situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment