a zip code crypto-currency system good for red ONLY

reduce.js 2.8KB

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