diff --git a/radar-commons-kotlin/src/main/kotlin/org/radarbase/kotlin/coroutines/flow/Extensions.kt b/radar-commons-kotlin/src/main/kotlin/org/radarbase/kotlin/coroutines/flow/Extensions.kt index 416cdfd0..66ee4b20 100644 --- a/radar-commons-kotlin/src/main/kotlin/org/radarbase/kotlin/coroutines/flow/Extensions.kt +++ b/radar-commons-kotlin/src/main/kotlin/org/radarbase/kotlin/coroutines/flow/Extensions.kt @@ -5,15 +5,21 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.zip +/** + * Convert a list of flows to one flow with a list of values. The list contains the latest value of + * each respective flow, in the same order as the original flows. + */ fun List>.zipAll(): Flow> = when (val numberOfFlows = size) { 0 -> flowOf(listOf()) 1 -> first().map { listOf(it) } 2 -> first().zip(last()) { a, b -> listOf(a, b) } - else -> subList(0, numberOfFlows - 1) - .zipAll() + else -> subList(0, numberOfFlows - 1).zipAll() .zip(last()) { rest, last -> rest + last } } +/** + * Alias of [List.zipAll]. + */ fun zipAll( vararg flows: Flow, ): Flow> = flows.toList().zipAll()