Reduce

reduce() will aggregate incoming values using given aggregate function:
  /*!*/import { value, spread, reduce } from '@connectv/core';

value([1, 2, 3, 4])
.to(spread())
/*!*/.to(reduce((total, each) => total + each))
.subscribe(console.log);

Initial value

When no initial value is passed to reduce() (like the example above), the first incoming value will be used as the initial value. You can provide an initial value like this:
  /*!*/import { value, spread, reduce } from '@connectv/core';

value([1, 2, 3, 4])
.to(spread())
/*!*/.to(reduce((total, each) => total * each, -1)) //--> so all values will be negative
.subscribe(console.log);

Function purity

The aggregate function MUST be pure, i.e. it should give the same result with the same inputs. Impure aggregate functions might result in unpredictable flow behavior.

Further reading




Copied to Clipboard!