runningReduce

fun <T> Flow<T>.runningReduce(operation: suspend (T, T) -> T): Flow<T>

Reduces the given flow with operation, emitting every intermediate result, including initial value. The first element is taken as initial value for operation accumulator. This operator has a sibling with initial value -- scan.

For example:

flowOf(1, 2, 3, 4).runningReduce { acc, value -> acc + value }.toList()

will produce [1, 3, 6, 10]

Sources

Link copied to clipboard