[d24f17c] | 1 | var _curry3 =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_curry3.js");
|
---|
| 4 |
|
---|
| 5 | var _xReduce =
|
---|
| 6 | /*#__PURE__*/
|
---|
| 7 | require("./internal/_xReduce.js");
|
---|
| 8 |
|
---|
| 9 | var _xwrap =
|
---|
| 10 | /*#__PURE__*/
|
---|
| 11 | require("./internal/_xwrap.js");
|
---|
| 12 | /**
|
---|
| 13 | * Returns a single item by iterating through the list, successively calling
|
---|
| 14 | * the iterator function and passing it an accumulator value and the current
|
---|
| 15 | * value from the array, and then passing the result to the next call.
|
---|
| 16 | *
|
---|
| 17 | * The iterator function receives two values: *(acc, value)*. It may use
|
---|
| 18 | * [`R.reduced`](#reduced) to shortcut the iteration.
|
---|
| 19 | *
|
---|
| 20 | * The arguments' order of [`reduceRight`](#reduceRight)'s iterator function
|
---|
| 21 | * is *(value, acc)*.
|
---|
| 22 | *
|
---|
| 23 | * Note: `R.reduce` does not skip deleted or unassigned indices (sparse
|
---|
| 24 | * arrays), unlike the native `Array.prototype.reduce` method. For more details
|
---|
| 25 | * on this behavior, see:
|
---|
| 26 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
|
---|
| 27 | *
|
---|
| 28 | * Be cautious of mutating and returning the accumulator. If you reuse it across
|
---|
| 29 | * invocations, it will continue to accumulate onto the same value. The general
|
---|
| 30 | * recommendation is to always return a new value. If you can't do so for
|
---|
| 31 | * performance reasons, then be sure to reinitialize the accumulator on each
|
---|
| 32 | * invocation.
|
---|
| 33 | *
|
---|
| 34 | * Dispatches to the `reduce` method of the third argument, if present. When
|
---|
| 35 | * doing so, it is up to the user to handle the [`R.reduced`](#reduced)
|
---|
| 36 | * shortcuting, as this is not implemented by `reduce`.
|
---|
| 37 | *
|
---|
| 38 | * @func
|
---|
| 39 | * @memberOf R
|
---|
| 40 | * @since v0.1.0
|
---|
| 41 | * @category List
|
---|
| 42 | * @sig ((a, b) -> a) -> a -> [b] -> a
|
---|
| 43 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the
|
---|
| 44 | * current element from the array.
|
---|
| 45 | * @param {*} acc The accumulator value.
|
---|
| 46 | * @param {Array} list The list to iterate over.
|
---|
| 47 | * @return {*} The final, accumulated value.
|
---|
| 48 | * @see R.reduced, R.addIndex, R.reduceRight
|
---|
| 49 | * @example
|
---|
| 50 | *
|
---|
| 51 | * R.reduce(R.subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
|
---|
| 52 | * // - -10
|
---|
| 53 | * // / \ / \
|
---|
| 54 | * // - 4 -6 4
|
---|
| 55 | * // / \ / \
|
---|
| 56 | * // - 3 ==> -3 3
|
---|
| 57 | * // / \ / \
|
---|
| 58 | * // - 2 -1 2
|
---|
| 59 | * // / \ / \
|
---|
| 60 | * // 0 1 0 1
|
---|
| 61 | *
|
---|
| 62 | * @symb R.reduce(f, a, [b, c, d]) = f(f(f(a, b), c), d)
|
---|
| 63 | */
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | var reduce =
|
---|
| 67 | /*#__PURE__*/
|
---|
| 68 | _curry3(function (xf, acc, list) {
|
---|
| 69 | return _xReduce(typeof xf === 'function' ? _xwrap(xf) : xf, acc, list);
|
---|
| 70 | });
|
---|
| 71 |
|
---|
| 72 | module.exports = reduce; |
---|