Created
December 5, 2022 21:44
-
-
Save joaorbrandao/7060c59b0ac0a145e2db9ddf13d062b1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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