flatMapIterableAsync

fun <T, R> Flow<T>.flatMapIterableAsync(concurrency: Int, transform: suspend (T) -> Iterable<R>): Flow<R>

Applies the given transformation function transform to each value of the original Flow and emits the results. The processing of items in the flow is concurrent and limited by concurrency level. Each transformed value is then flattened and emitted individually.

The result flow emits the elements in an ordered manner.

Example usage:

flowOf(1, 2, 3)
.flatMapIterableAsync(2) { listOf(it, it + 1) }
.collect { println(it) } // Prints 1, 2, 2, 3, 3, 4

Return

A new flow with the transformed and flattened items.

Parameters

concurrency

The maximum number of concurrent transformations.

transform

The transformation function to apply to each item.


fun <T, R, P> Flow<T>.flatMapIterableAsync(semaphore: suspend CoroutineScope.() -> AsyncSemaphore<P>, transform: suspend (T) -> Iterable<R>): Flow<R>

Applies the given transformation function transform to each value of the original Flow and emits the results. The concurrency is managed by an AsyncSemaphore created by the semaphore suspending function. Each transformed value is then flattened and emitted individually.

The result flow emits the elements in an ordered manner.

Example usage:

val customSemaphore: suspend CoroutineScope.() -> AsyncSemaphore =  { AsyncSemaphore(this, 2) }

flowOf(1, 2, 3)
.flatMapIterableAsync(customSemaphore) { listOf(it, it + 1) }
.collect { println(it) } // Prints the results in an ordered manner

Return

A new flow with the transformed and flattened items.

Parameters

semaphore

The suspending function that creates an AsyncSemaphore.

transform

The transformation function to apply to each item.


suspend fun <T, R> Iterable<T>.flatMapIterableAsync(transform: suspend (T) -> Iterable<R>): List<R>

Transforms the elements of the iterable concurrently using the provided transform function, and then flattens the result.

Return

A List containing the flattened results of applying transform to each element of the iterable.

Parameters

transform

A suspend function to apply to each element of the iterable.


suspend fun <T, R> Iterable<T>.flatMapIterableAsync(concurrency: Int, transform: suspend (T) -> Iterable<R>): List<R>

Transforms the elements of the iterable concurrently using the provided transform function with a specified concurrency limit, and then flattens the result.

Return

A List containing the flattened results of applying transform to each element of the iterable.

Parameters

concurrency

The maximum number of concurrent transformations.

transform

A suspend function to apply to each element of the iterable.