source: node_modules/ramda/src/reduceRight.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.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 * 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)*. `reduceRight` may use [`reduced`](#reduced)
14 * to short circuit the iteration.
15 *
16 * Note: `R.reduceRight` does not skip deleted or unassigned indices (sparse
17 * arrays), unlike the native `Array.prototype.reduceRight` method. For more details
18 * on this behavior, see:
19 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description
20 *
21 * Be cautious of mutating and returning the accumulator. If you reuse it across
22 * invocations, it will continue to accumulate onto the same value. The general
23 * recommendation is to always return a new value. If you can't do so for
24 * performance reasons, then be sure to reinitialize the accumulator on each
25 * invocation.
26 *
27 * @func
28 * @memberOf R
29 * @since v0.1.0
30 * @category List
31 * @sig ((a, b) -> b) -> b -> [a] -> b
32 * @param {Function} fn The iterator function. Receives two values, the current element from the array
33 * and the accumulator.
34 * @param {*} acc The accumulator value.
35 * @param {Array} list The list to iterate over.
36 * @return {*} The final, accumulated value.
37 * @see R.reduce, R.addIndex, R.reduced
38 * @example
39 *
40 * R.reduceRight(R.subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2
41 * // - -2
42 * // / \ / \
43 * // 1 - 1 3
44 * // / \ / \
45 * // 2 - ==> 2 -1
46 * // / \ / \
47 * // 3 - 3 4
48 * // / \ / \
49 * // 4 0 4 0
50 *
51 * @symb R.reduceRight(f, a, [b, c, d]) = f(b, f(c, f(d, a)))
52 */
53
54
55var reduceRight =
56/*#__PURE__*/
57_curry3(function reduceRight(fn, acc, list) {
58 var idx = list.length - 1;
59
60 while (idx >= 0) {
61 acc = fn(list[idx], acc);
62
63 if (acc && acc['@@transducer/reduced']) {
64 acc = acc['@@transducer/value'];
65 break;
66 }
67
68 idx -= 1;
69 }
70
71 return acc;
72});
73
74module.exports = reduceRight;
Note: See TracBrowser for help on using the repository browser.