Skip to content

Instantly share code, notes, and snippets.

@gre
Forked from sadache/code.scala
Created December 12, 2011 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gre/1466196 to your computer and use it in GitHub Desktop.
Save gre/1466196 to your computer and use it in GitHub Desktop.
Play20 Promise sequence function
// 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