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