Skip to content

Instantly share code, notes, and snippets.

@iamkatrechko
Created September 13, 2018 05:00
Show Gist options
  • Save iamkatrechko/6e2dc88bc6146e048ee841e80551cf86 to your computer and use it in GitHub Desktop.
Save iamkatrechko/6e2dc88bc6146e048ee841e80551cf86 to your computer and use it in GitHub Desktop.
suspend fun <A, B> Collection<A>.parallelMap(
context: CoroutineContext = DefaultDispatcher,
block: suspend (A) -> B
): Collection<B> {
return map {
// Use async to start a coroutine for each item
async(context) {
block(it)
}
}.map {
// We now have a map of Deferred<T> so we await() each
it.await()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment