1 | import { addIndex, reduce } from 'ramda';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * {@link http://ramdajs.com/docs/#reduce|R.reduce} function that more closely resembles Array.prototype.reduce.
|
---|
5 | * It takes two new parameters to its callback function: the current index, and the entire list.
|
---|
6 | *
|
---|
7 | * `reduceIndexed` implementation is simple : `
|
---|
8 | * const reduceIndexed = R.addIndex(R.reduce);
|
---|
9 | * `
|
---|
10 | * @func reduceIndexed
|
---|
11 | * @memberOf RA
|
---|
12 | * @since {@link https://char0n.github.io/ramda-adjunct/2.5.0|v2.5.0}
|
---|
13 | * @category List
|
---|
14 | * @typedef Idx = Number
|
---|
15 | * @sig ((a, b, Idx, [b]) => a) -> a -> [b] -> a
|
---|
16 | * @param {Function} fn The iterator function. Receives four values,
|
---|
17 | * the accumulator, the current element from the array, index and the entire list
|
---|
18 | * @param {*} acc The accumulator value
|
---|
19 | * @param {Array} list The list to iterate over
|
---|
20 | * @return {*} The final, accumulated value
|
---|
21 | * @see {@link http://ramdajs.com/docs/#addIndex|R.addIndex}, {@link http://ramdajs.com/docs/#reduce|R.reduce}
|
---|
22 | * @example
|
---|
23 | *
|
---|
24 | * const initialList = ['f', 'o', 'o', 'b', 'a', 'r'];
|
---|
25 | *
|
---|
26 | * reduceIndexed((acc, val, idx, list) => acc + '-' + val + idx, '', initialList);
|
---|
27 | * //=> "-f0-o1-o2-b3-a4-r5"
|
---|
28 | */
|
---|
29 | const reduceIndexed = addIndex(reduce);
|
---|
30 |
|
---|
31 | export default reduceIndexed;
|
---|