Skip to content

Instantly share code, notes, and snippets.

@sungjk
Created August 10, 2018 15:56
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 sungjk/332f91d3bf9a7f2cb2c0992445091f61 to your computer and use it in GitHub Desktop.
Save sungjk/332f91d3bf9a7f2cb2c0992445091f61 to your computer and use it in GitHub Desktop.
`groupedCollect` helper method for parallelism
object future {
def groupedCollect[A, B](xs: Seq[A], par: Int)(f: A => Future[B]): Future[Seq[B]] = {
val bsF: Future[Seq[B]] = Future.value(Seq.empty[B])
xs.grouped(par).foldLeft(bsF){ case (bsF, group) => {
for {
bs <- bsF
xs <- Future.collect(group.map(f))
} yield bs ++ xs
}}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment