Created
September 13, 2018 05:00
-
-
Save iamkatrechko/6e2dc88bc6146e048ee841e80551cf86 to your computer and use it in GitHub Desktop.
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
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