scan

fun <T, R> Flow<T>.scan(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).scan(emptyList<Int>()) { acc, value -> acc + value }.toList()

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

This function is an alias to runningFold operator.

Sources

common source
Link copied to clipboard