Skip to content

Commit

Permalink
Added javadoc to List<Flow<T>>.zipAll
Browse files Browse the repository at this point in the history
  • Loading branch information
blootsvoets committed Sep 12, 2023
1 parent 1c46b79 commit 1c9731d
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> List<Flow<T>>.zipAll(): Flow<List<T>> = 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 <T> zipAll(
vararg flows: Flow<T>,
): Flow<List<T>> = flows.toList().zipAll()

0 comments on commit 1c9731d

Please sign in to comment.