Skip to content

Instantly share code, notes, and snippets.

@cameronhotchkies
Created June 19, 2015 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronhotchkies/eeac5487ae6f3db0bed3 to your computer and use it in GitHub Desktop.
Save cameronhotchkies/eeac5487ae6f3db0bed3 to your computer and use it in GitHub Desktop.
Like Future.sequence but discards all Future.failure results
def happySequence[A](source: Seq[Future[A]]) = {
val optioned = source.map { f =>
f.map(Option.apply).recover {
case _ => None
}
}
Future.sequence(optioned).map(_.flatten)
}
/* Example Usage */
val f1 = Future.successful(1)
val f2 = Future.successful(2)
val f3 = Future.failed(new Exception())
val l1 = List(f1, f2, f3)
val fl = happySequence(l1)
fl.onComplete(ff => println(ff))
// Optional:
Thread.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment