rrva
fun <A, B> Iterable<A>.parallelMap(context: CoroutineContext, f: suspend (A) -> B): List<B> = runBlocking { map { async(context) { f(it) } }.map { it.await() } } @OptIn(ExperimentalCoroutinesApi::class) private val apiPool = Dispatchers.IO.limitedParallelism(10) val results = foo.parallelMap(apiPool) { fetchStuffFromRestAPI(it.id) }
Sam
parallelMap
suspend
suspend fun <A, B> Iterable<A>.parallelMap(f: suspend (A) -> B): List<B> = coroutineScope { map { async { f(it) } }.map { it.await() } }
.map { it.await() }
.awaitAll()
A modern programming language that makes developers happier.