UNPKG

1.98 kBJavaScriptView Raw
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3");
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 * Similar to [`reduce`](#reduce), except moves through the input list from the
10 * right to the left.
11 *
12 * The iterator function receives two values: *(value, acc)*, while the arguments'
13 * order of `reduce`'s iterator function is *(acc, value)*.
14 *
15 * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
16 * arrays), unlike the native `Array.prototype.reduceRight` method. For more details
17 * on this behavior, see:
18 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
19 *
20 * @func
21 * @memberOf R
22 * @since v0.1.0
23 * @category List
24 * @sig ((a, b) -> b) -> b -> [a] -> b
25 * @param {Function} fn The iterator function. Receives two values, the current element from the array
26 * and the accumulator.
27 * @param {*} acc The accumulator value.
28 * @param {Array} list The list to iterate over.
29 * @return {*} The final, accumulated value.
30 * @see R.reduce, R.addIndex
31 * @example
32 *
33 * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
34 * // - -2
35 * // / \ / \
36 * // 1 - 1 3
37 * // / \ / \
38 * // 2 - ==> 2 -1
39 * // / \ / \
40 * // 3 - 3 4
41 * // / \ / \
42 * // 4 0 4 0
43 *
44 * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
45 */
46
47
48var reduceRight =
49/*#__PURE__*/
50_curry3(function reduceRight(fn, acc, list) {
51 var idx = list.length - 1;
52
53 while (idx >= 0) {
54 acc = fn(list[idx], acc);
55 idx -= 1;
56 }
57
58 return acc;
59});
60
61module.exports = reduceRight;
\No newline at end of file