UNPKG

1.53 kBJavaScriptView Raw
1var _arity =
2/*#__PURE__*/
3require("./internal/_arity");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8
9var head =
10/*#__PURE__*/
11require("./head");
12
13var _reduce =
14/*#__PURE__*/
15require("./internal/_reduce");
16
17var tail =
18/*#__PURE__*/
19require("./tail");
20
21var identity =
22/*#__PURE__*/
23require("./identity");
24/**
25 * Performs left-to-right function composition using transforming function. The first argument may have
26 * any arity; the remaining arguments must be unary.
27 *
28 * **Note:** The result of pipeWith is not automatically curried. Transforming function is not used on the
29 * first argument.
30 *
31 * @func
32 * @memberOf R
33 * @since v0.26.0
34 * @category Function
35 * @sig ((* -> *), [((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)]) -> ((a, b, ..., n) -> z)
36 * @param {...Function} functions
37 * @return {Function}
38 * @see R.composeWith, R.pipe
39 * @example
40 *
41 * const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
42 * const f = pipeWhileNotNil([Math.pow, R.negate, R.inc])
43 *
44 * f(3, 4); // -(3^4) + 1
45 * @symb R.pipeWith(f)([g, h, i])(...args) = f(i, f(h, g(...args)))
46 */
47
48
49var pipeWith =
50/*#__PURE__*/
51_curry2(function pipeWith(xf, list) {
52 if (list.length <= 0) {
53 return identity;
54 }
55
56 var headList = head(list);
57 var tailList = tail(list);
58 return _arity(headList.length, function () {
59 return _reduce(function (result, f) {
60 return xf.call(this, f, result);
61 }, headList.apply(this, arguments), tailList);
62 });
63});
64
65module.exports = pipeWith;
\No newline at end of file