Skip to content

Instantly share code, notes, and snippets.

@joaorbrandao
Created December 5, 2022 21:44
Show Gist options
  • Save joaorbrandao/7060c59b0ac0a145e2db9ddf13d062b1 to your computer and use it in GitHub Desktop.
Save joaorbrandao/7060c59b0ac0a145e2db9ddf13d062b1 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext
suspend fun <A, B> Iterable<A>.pmap(
f: suspend (A) -> B,
): List<B> = withContext(Dispatchers.IO) {
map { async { f(it) } }.awaitAll()
}
// Usage:
fun main() = runBlocking {
listOf(1, 2, 3).pmap { /* async stuff to process */ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment