Episode 21 - The scan operator Monday, 5 Feb.
❚ scan takes three arguments:
- an input stream
- an accumulator function (e.g. prepend a
- an initial value called seed (e.g.
The accumulator function combines two values:
- an accumulated value called
- and a
As a result, the accumulator returns a
This is how
❚ scan works with an input stream, an accumulator and a seed:
- The seed will be used as an initial
- When the input stream emits an event value
vand the latest
accare given to the accumulator
- The returned value is emitted and will be used as the next
As a result,
❚ scan returns a new stream of accumulated values.
- As you can see in the lesson, the output stream does not start with the seed. This is how it works, for example, in RxJS.
- In some other reactive stream libraries (like xstream and Most.js) the output stream starts with the seed.
- To start with the seed in RxJS and such, you can chain
❚ scan(f, seed).