reduce

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 will be used as the next acc
  • When the input stream completes, the output stream emits the last acc and completes

returns a new stream of accumulated values

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, 12 Feb. 2018
Revised on Monday, 26 Nov. 2018

❚ reduce takes the same three arguments as ❚ scan:

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

Learn more about scan

Notes

As you can see, while ❚ scan returns a new stream of progressively accumulated values, ❚ reduce returns a new stream of, at most, one value. At most? If the input stream never completes, ❚ reduce will never emit any value on the output stream.

Ultimately, ❚ reduce is equivalent to ❚ scan chained with ❚ takeLast(1).

See also


accumulator


The JavaScript pipeline operator proposal


Launchpad for RxJS

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 →