Repository URL to install this package:
|
Version:
6.0.4 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fromArgumentsToArray_1 = require("../../cast/fromArgumentsToArray");
var curry_1 = require("../../fp/curry");
var max_1 = require("../../math/max");
var _reduce_1 = require("./_reduce");
var pluck_1 = require("./pluck");
var _map_1 = require("./_map");
/**
* Accepts a converging function and a list of branching functions and returns
* a new function. When invoked, this new function is applied to some
* arguments, each branching function is applied to those same arguments. The
* results of each branching function are passed as arguments to the converging
* function to produce the return value.
* @since 5.0.0-beta.6
* @memberOf fp
*
* @param {Function} after A function. `after` will be invoked with the return values of
* `fn1` and `fn2` as its arguments.
* @param {Array} functions A list of functions.
* @return {Function} A new function.
*
* @NOTE important to use 2+ functions in functions param
*
* @func
* @fork v0.4.2
* @category Function
* @sig ((x1, x2, ...) -> z) -> [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> (a -> b -> ... -> z)
* @symb converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
*
* @see useWith
*
* @example
*
* var average = converge(divide, [sum, length])
* average([1, 2, 3, 4, 5, 6, 7]) //=> 4
*
* var strangeConcat = converge(concat, [toUpper, toLower])
* strangeConcat("Yodel") //=> "YODELyodel"
*
*/
function _converge(after, fns) {
var num = _reduce_1.default(max_1.default, 0, pluck_1.default('length', fns));
return curry_1.default(num, function () {
var args = fromArgumentsToArray_1.fromArgumentsToArray.apply(void 0, arguments);
var self = this;
var index = 0;
return after.apply(self, _map_1.default(function (fn) {
fn = fn || fns[index++];
// console.log({fn, args, fns, after, index, i: fns[index++]})
return fn.apply(self, args);
}, fns));
});
}
var converge = curry_1.default(2, _converge);
exports.default = converge;
//# sourceMappingURL=converge.js.map