Shreyas Patil
inline fun <T, R> Flow<T>.concurrentMap( concurrency: Int, @BuilderInference crossinline block: suspend (T) -> R ) = flow<R> { val scope = CoroutineScope(Executors.newFixedThreadPool(concurrency).asCoroutineDispatcher()) val deferredResults = mutableListOf<Deferred<R>>() collect { value -> deferredResults.add(scope.async { block(value) }) } deferredResults.awaitAll().forEach { newValue -> emit(newValue) } scope.cancel() }
Sam
flatMapMerge
inline fun <T, R> Flow<T>.concurrentMap( concurrency: Int, @BuilderInference crossinline block: suspend (T) -> R ) = flatMapMerge(concurrency) { value -> flow { emit(block(value)) } }
A modern programming language that makes developers happier.