reactive.how 2.0-alpha Menu

scan

Scan arguments

❚ scan takes three arguments:

  • an input stream
  • an accumulator function (e.g. prepend a character to a string)
  • an initial value called seed (e.g. !)

The accumulator function combines two values:

  • an accumulated value called accumulation
  • and a value

As a result, the accumulator returns a new accumulation.

Scan operation

This is how ❚ scan works with an input stream, an accumulator and a seed:

  • The seed will be used as an initial acc (accumulation)
  • When the input stream emits an event value v:
    • v and the latest acc are given to the accumulator
    • The returned value is emitted and will be used as the next acc

As a result, ❚ scan returns a new stream of accumulated values.

Notes

  • As you can see, 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 ❚ startWith(seed) and ❚ scan(f, seed).

Next


combineLatest vs merge

See also


reduce vs scan


accumulator

💌 I create something new each week!
Learn Reactive Programming and stay tuned.

Occasional updates, plus:

Cédric Soulas - Freelance Developer Advocate. Learn more.

Follow 👨‍💻 Hire me

News

@CedricSoulas