scan

accepts 1 input stream, an accumulator, a seed (optional)

  • The seed will be used as an initial accumulation acc
  • When the input stream emits a 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

returns a new stream of accumulated values

Published on Monday, 5 Feb. 2018
Revised on Monday, 26 Nov. 2018

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.

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).
Cédric Soulas Follow Hire me

Freelance Developer Advocate. Motion graphics with code. JavaScript and Elm. cedricsoulas.com

Subscribe to reactive.how newsletter

Join the Newsletter

Learn Reactive Programming and stay up-to-date:

Receive my latest news, product updates and programming visualizations. You can unsubscribe at any time.

Highlights

@CedricSoulas

Making an illustrated book!

The Illustrated Book of RxJS

Learn more →