Skip to content

Instantly share code, notes, and snippets.

@clojj
Created August 8, 2020 18:08
Show Gist options
  • Save clojj/bf8f2ae5392a2be41e31b71259ef4c6f to your computer and use it in GitHub Desktop.
Save clojj/bf8f2ae5392a2be41e31b71259ef4c6f to your computer and use it in GitHub Desktop.
parallel map in Kotlin
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.awaitAll
suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment