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

Subscribe to reactive.how newsletter

Receive my latest news, tips and animations:

Learn Reactive Programming and stay up-to-date:
Cédric Soulas

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