Collecting values on flow: fold and scan
This is a chapter from the book Kotlin Coroutines. You can find it on LeanPub.
If you use collection processing functions, you might recognize fold
. It is used to combine all the values in this collection into one by applying an operation that combines two values into one for each element (starting from the initial value).
For example, if the initial value is 0
and the operation is addition, then the result is the sum of all the numbers: we first take the initial value 0
; then, we add the first element 1
to it; to the result 1
, we add the second number 2
; to the result 3
, we add the third number 3
; to the result 6
, we add the last number 4
. The result of this operation, 10
, is what will be returned from fold
.
fold
is a terminal operation. It can also be used for Flow, but it will suspend until this flow is completed (just like collect
).
There is an alternative to fold
called scan
. It is an intermediate operation that produces all intermediate accumulator values.
scan
is useful withFlow
because it produces a new value immediately after receiving one from the previous step.
We can implement scan
easily using the flow
builder and collect
. We first emit the initial value, then with each new element we emit the result of the next value accumulation.
The typical use case for scan
is when we have a flow of updates or changes, and we need an object that is the result of these changes.