runningFold

fun <T, R> Flow<T>.runningFold(initial: R, operation: suspend (R, T) -> R): Flow<R>

Folds the given flow with operation, emitting every intermediate result, including initial value. Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors. For example:

flowOf(1, 2, 3).runningFold(emptyList<Int>()) { acc, value -> acc + value }.toList()

will produce [], [1], [1, 2], [1, 2, 3]].

Sources

common source
Link copied to clipboard