UNPKG

1.83 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4
5var curryN =
6/*#__PURE__*/
7require("./curryN");
8/**
9 * Accepts a function `fn` and a list of transformer functions and returns a
10 * new curried function. When the new function is invoked, it calls the
11 * function `fn` with parameters consisting of the result of calling each
12 * supplied handler on successive arguments to the new function.
13 *
14 * If more arguments are passed to the returned function than transformer
15 * functions, those arguments are passed directly to `fn` as additional
16 * parameters. If you expect additional arguments that don't need to be
17 * transformed, although you can ignore them, it's best to pass an identity
18 * function so that the new function reports the correct arity.
19 *
20 * @func
21 * @memberOf R
22 * @since v0.1.0
23 * @category Function
24 * @sig ((x1, x2, ...) -> z) -> [(a -> x1), (b -> x2), ...] -> (a -> b -> ... -> z)
25 * @param {Function} fn The function to wrap.
26 * @param {Array} transformers A list of transformer functions
27 * @return {Function} The wrapped function.
28 * @see R.converge
29 * @example
30 *
31 * R.useWith(Math.pow, [R.identity, R.identity])(3, 4); //=> 81
32 * R.useWith(Math.pow, [R.identity, R.identity])(3)(4); //=> 81
33 * R.useWith(Math.pow, [R.dec, R.inc])(3, 4); //=> 32
34 * R.useWith(Math.pow, [R.dec, R.inc])(3)(4); //=> 32
35 * @symb R.useWith(f, [g, h])(a, b) = f(g(a), h(b))
36 */
37
38
39var useWith =
40/*#__PURE__*/
41_curry2(function useWith(fn, transformers) {
42 return curryN(transformers.length, function () {
43 var args = [];
44 var idx = 0;
45
46 while (idx < transformers.length) {
47 args.push(transformers[idx].call(this, arguments[idx]));
48 idx += 1;
49 }
50
51 return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, transformers.length)));
52 });
53});
54
55module.exports = useWith;
\No newline at end of file