Skip to content

Instantly share code, notes, and snippets.

@martin-tarjanyi
Created June 15, 2024 20:58
Show Gist options
  • Select an option

  • Save martin-tarjanyi/9b843e99e45abc6a200fe51e5e1eb0f4 to your computer and use it in GitHub Desktop.

Select an option

Save martin-tarjanyi/9b843e99e45abc6a200fe51e5e1eb0f4 to your computer and use it in GitHub Desktop.
Kotlin flow concurrent map
fun <T, R> Flow<T>.concurrentMap(
concurrency: Int,
dispatcher: CoroutineDispatcher = Dispatchers.Unconfined,
transform: suspend (value: T) -> R,
): Flow<R> =
map { flow { emit(transform(it)) }.flowOn(dispatcher) }
.flattenMerge(concurrency)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment