Created
June 19, 2015 18:02
-
-
Save cameronhotchkies/eeac5487ae6f3db0bed3 to your computer and use it in GitHub Desktop.
Like Future.sequence but discards all Future.failure results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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