-
-
Save gre/1466196 to your computer and use it in GitHub Desktop.
Play20 Promise sequence function
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
// What is a Promise sequence function ? | |
// A function which transform a List[Promise[A]] into a Promise[List[A]] | |
// Usage: | |
// val list: List[Promise[Int]] = ... | |
// val sequenced: Promise[List[Int]] = list.sequence() // got it :) | |
package utils | |
import play.api.libs.concurrent._ | |
object `package`{ | |
implicit def listToPromiseSequencableList[A](list: List[Promise[A]]) = new PromiseSequencableList[A](list) | |
} | |
class PromiseSequencableList[A](list: List[Promise[A]]) { | |
def sequence(): Promise[List[A]] = list.foldLeft(Promise.pure(List[A]()))((s,p) => s.flatMap( s => p.map(a => s :+ a))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment