reduce.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { scan } from './scan';
  2. import { takeLast } from './takeLast';
  3. import { defaultIfEmpty } from './defaultIfEmpty';
  4. import { pipe } from '../util/pipe';
  5. /* tslint:enable:max-line-length */
  6. /**
  7. * Applies an accumulator function over the source Observable, and returns the
  8. * accumulated result when the source completes, given an optional seed value.
  9. *
  10. * <span class="informal">Combines together all values emitted on the source,
  11. * using an accumulator function that knows how to join a new source value into
  12. * the accumulation from the past.</span>
  13. *
  14. * <img src="./img/reduce.png" width="100%">
  15. *
  16. * Like
  17. * [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce),
  18. * `reduce` applies an `accumulator` function against an accumulation and each
  19. * value of the source Observable (from the past) to reduce it to a single
  20. * value, emitted on the output Observable. Note that `reduce` will only emit
  21. * one value, only when the source Observable completes. It is equivalent to
  22. * applying operator {@link scan} followed by operator {@link last}.
  23. *
  24. * Returns an Observable that applies a specified `accumulator` function to each
  25. * item emitted by the source Observable. If a `seed` value is specified, then
  26. * that value will be used as the initial value for the accumulator. If no seed
  27. * value is specified, the first item of the source is used as the seed.
  28. *
  29. * @example <caption>Count the number of click events that happened in 5 seconds</caption>
  30. * var clicksInFiveSeconds = Rx.Observable.fromEvent(document, 'click')
  31. * .takeUntil(Rx.Observable.interval(5000));
  32. * var ones = clicksInFiveSeconds.mapTo(1);
  33. * var seed = 0;
  34. * var count = ones.reduce((acc, one) => acc + one, seed);
  35. * count.subscribe(x => console.log(x));
  36. *
  37. * @see {@link count}
  38. * @see {@link expand}
  39. * @see {@link mergeScan}
  40. * @see {@link scan}
  41. *
  42. * @param {function(acc: R, value: T, index: number): R} accumulator The accumulator function
  43. * called on each source value.
  44. * @param {R} [seed] The initial accumulation value.
  45. * @return {Observable<R>} An Observable that emits a single value that is the
  46. * result of accumulating the values emitted by the source Observable.
  47. * @method reduce
  48. * @owner Observable
  49. */
  50. export function reduce(accumulator, seed) {
  51. // providing a seed of `undefined` *should* be valid and trigger
  52. // hasSeed! so don't use `seed !== undefined` checks!
  53. // For this reason, we have to check it here at the original call site
  54. // otherwise inside Operator/Subscriber we won't know if `undefined`
  55. // means they didn't provide anything or if they literally provided `undefined`
  56. if (arguments.length >= 2) {
  57. return function reduceOperatorFunctionWithSeed(source) {
  58. return pipe(scan(accumulator, seed), takeLast(1), defaultIfEmpty(seed))(source);
  59. };
  60. }
  61. return function reduceOperatorFunction(source) {
  62. return pipe(scan((acc, value, index) => {
  63. return accumulator(acc, value, index + 1);
  64. }), takeLast(1))(source);
  65. };
  66. }
  67. //# sourceMappingURL=reduce.js.map