forked from waja/action-debian-package
42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
|
import _curryN from "./internal/_curryN.js";
|
||
|
import _reduce from "./internal/_reduce.js";
|
||
|
import _reduced from "./internal/_reduced.js";
|
||
|
/**
|
||
|
* Like [`reduce`](#reduce), `reduceWhile` returns a single item by iterating
|
||
|
* through the list, successively calling the iterator function. `reduceWhile`
|
||
|
* also takes a predicate that is evaluated before each step. If the predicate
|
||
|
* returns `false`, it "short-circuits" the iteration and returns the current
|
||
|
* value of the accumulator.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.22.0
|
||
|
* @category List
|
||
|
* @sig ((a, b) -> Boolean) -> ((a, b) -> a) -> a -> [b] -> a
|
||
|
* @param {Function} pred The predicate. It is passed the accumulator and the
|
||
|
* current element.
|
||
|
* @param {Function} fn The iterator function. Receives two values, the
|
||
|
* accumulator and the current element.
|
||
|
* @param {*} a The accumulator value.
|
||
|
* @param {Array} list The list to iterate over.
|
||
|
* @return {*} The final, accumulated value.
|
||
|
* @see R.reduce, R.reduced
|
||
|
* @example
|
||
|
*
|
||
|
* const isOdd = (acc, x) => x % 2 === 1;
|
||
|
* const xs = [1, 3, 5, 60, 777, 800];
|
||
|
* R.reduceWhile(isOdd, R.add, 0, xs); //=> 9
|
||
|
*
|
||
|
* const ys = [2, 4, 6]
|
||
|
* R.reduceWhile(isOdd, R.add, 111, ys); //=> 111
|
||
|
*/
|
||
|
|
||
|
var reduceWhile =
|
||
|
/*#__PURE__*/
|
||
|
_curryN(4, [], function _reduceWhile(pred, fn, a, list) {
|
||
|
return _reduce(function (acc, x) {
|
||
|
return pred(acc, x) ? fn(acc, x) : _reduced(acc);
|
||
|
}, a, list);
|
||
|
});
|
||
|
|
||
|
export default reduceWhile;
|