reduce

1 input stream accumulator seed (opt.)

  • 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

1 input stream accumulator seed (opt.)

  • 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

❚ 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


takeLast vs take


count


accumulator


The JavaScript pipeline operator proposal

💌 Learn Reactive Programming and stay tuned

Occasional updates, plus:

News

@CedricSoulas