a zip code crypto-currency system good for red ONLY

reduce.js 3.2KB

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