https://kotlinlang.org logo
Title
r

rrva

08/19/2022, 10:07 AM
How can this be rewritten in a better way wrt structured concurrency and flow etc
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) }
s

Sam

08/19/2022, 10:10 AM
For structured concurrency it would be best to change the
parallelMap
function to a
suspend
function:
suspend fun <A, B> Iterable<A>.parallelMap(f: suspend (A) -> B): List<B> = coroutineScope {
    map { async { f(it) } }.map { it.await() }
}
I wouldn’t introduce flows here; I don’t think it would help you
You could also swap
.map { it.await() }
for
.awaitAll()
which will fail fast if one of the child jobs fails — depends on your approach to the structured concurrency
r

rrva

08/19/2022, 11:04 AM
if I want to convert failures to null, just try-catch inside the lambda is cleanest?
s

Sam

08/19/2022, 12:40 PM
Yes 👍