Skip to content

Instantly share code, notes, and snippets.

@slaypni
Last active January 9, 2020 11:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slaypni/7bceb24118f6fc03f757ab01f1c1eb04 to your computer and use it in GitHub Desktop.
Save slaypni/7bceb24118f6fc03f757ab01f1c1eb04 to your computer and use it in GitHub Desktop.
Parallel map for kotlin
import kotlinx.coroutines.experimental.async
import kotlin.coroutines.experimental.CoroutineContext
suspend fun <T, R> Iterable<T>.pmap(context: CoroutineContext, transform: suspend (T) -> R): List<R> {
return this.map {
async(context) {
transform(it)
}
}.map { it.await() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment