reduce.js 2.7 KB

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