mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 13:11:31 +03:00
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/converge.js
|
||
|
|
||
|
|
||
|
import _curry2 from './internal/_curry2.js';
|
||
|
import _map from './internal/_map.js';
|
||
|
import curryN from './curryN.js';
|
||
|
import max from './max.js';
|
||
|
import pluck from './pluck.js';
|
||
|
import reduce from './reduce.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Accepts a converging function and a list of branching functions and returns
|
||
|
* a new function. The arity of the new function is the same as the arity of
|
||
|
* the longest branching function. When invoked, this new function is applied
|
||
|
* to some arguments, and 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.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.4.2
|
||
|
* @category Function
|
||
|
* @sig ((x1, x2, ...) -> z) -> [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> (a -> b -> ... -> z)
|
||
|
* @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.
|
||
|
* @see R.useWith
|
||
|
* @example
|
||
|
*
|
||
|
* const average = R.converge(R.divide, [R.sum, R.length])
|
||
|
* average([1, 2, 3, 4, 5, 6, 7]) //=> 4
|
||
|
*
|
||
|
* const strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
|
||
|
* strangeConcat("Yodel") //=> "YODELyodel"
|
||
|
*
|
||
|
* @symb R.converge(f, [g, h])(a, b) = f(g(a, b), h(a, b))
|
||
|
*/
|
||
|
var converge = _curry2(function converge(after, fns) {
|
||
|
return curryN(reduce(max, 0, pluck('length', fns)), function() {
|
||
|
var args = arguments;
|
||
|
var context = this;
|
||
|
return after.apply(context, _map(function(fn) {
|
||
|
return fn.apply(context, args);
|
||
|
}, fns));
|
||
|
});
|
||
|
});
|
||
|
export default converge;
|