source: node_modules/ramda-adjunct/src/reduceRightP.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: 3.3 KB
Line 
1import { curryN, pipe, equals, reduceRight, length, concat } from 'ramda';
2
3import isUndefined from './isUndefined';
4import resolveP from './resolveP';
5import allP from './allP';
6
7// in older ramda versions the order of the arguments is flipped
8const flipArgs = pipe(reduceRight(concat, ''), equals('ba'))(['a', 'b']);
9
10/* eslint-disable max-len */
11/**
12 * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
13 * which produces promises (or a mix of promises and values),
14 * iterate over all the values in the `Iterable` into an array and
15 * reduce the array to a value using the given iterator function.
16 *
17 * Similar to {@link RA.reduceP|reduceP} except moves through the input list from the right to the left.
18 * The iterator function receives two values: (value, acc),
19 * while the arguments' order of reduceP's iterator function is (acc, value).
20 *
21 * @func reduceRightP
22 * @memberOf RA
23 * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0}
24 * @category List
25 * @typedef MaybePromise = Promise.<*> | *
26 * @sig ((MaybePromise b, Promise a) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
27 * @param {Function} fn The iterator function. Receives two values, the current element from the list and the accumulator
28 * @param {*|Promise.<*>} acc The accumulator value
29 * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over
30 * @return {Promise} The final, accumulated value
31 * @see {@link RA.reduceP|reduceP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
32 * @example
33 *
34 * RA.reduceRightP(
35 * (fileName, total) => fs
36 * .readFileAsync(fileName, 'utf8')
37 * .then(contents => total + parseInt(contents, 10)),
38 * 0,
39 * ['file1.txt', 'file2.txt', 'file3.txt']
40 * ); // => Promise(10)
41 *
42 * RA.reduceRightP(
43 * (fileName, total) => fs
44 * .readFileAsync(fileName, 'utf8')
45 * .then(contents => total + parseInt(contents, 10)),
46 * Promise.resolve(0),
47 * ['file1.txt', 'file2.txt', 'file3.txt']
48 * ); // => Promise(10)
49 *
50 * RA.reduceRightP(
51 * (fileName, total) => fs
52 * .readFileAsync(fileName, 'utf8')
53 * .then(contents => total + parseInt(contents, 10)),
54 * 0,
55 * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']
56 * ); // => Promise(10)
57 *
58 * RA.reduceRightP(
59 * (fileName, total) => fs
60 * .readFileAsync(fileName, 'utf8')
61 * .then(contents => total + parseInt(contents, 10)),
62 * 0,
63 * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'])
64 * ); // => Promise(10)
65 *
66 */
67/* esline-enable max-len */
68const reduceRightP = curryN(3, (fn, acc, list) =>
69 resolveP(list).then((iterable) => {
70 const listLength = length(iterable);
71
72 if (listLength === 0) {
73 return acc;
74 }
75
76 const reducer = reduceRight((arg1, arg2) => {
77 let accP;
78 let currentValueP;
79
80 if (flipArgs) {
81 [accP, currentValueP] = [arg1, arg2];
82 } else {
83 [accP, currentValueP] = [arg2, arg1];
84 }
85
86 return accP
87 .then((previousValue) => allP([previousValue, currentValueP]))
88 .then(([previousValue, currentValue]) => {
89 if (isUndefined(previousValue) && listLength === 1) {
90 return currentValue;
91 }
92
93 return fn(currentValue, previousValue);
94 });
95 });
96
97 return reducer(resolveP(acc), iterable);
98 })
99);
100
101export default reduceRightP;
Note: See TracBrowser for help on using the repository browser.