pipeWith.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import _arity from "./internal/_arity.js";
  2. import _curry2 from "./internal/_curry2.js";
  3. import head from "./head.js";
  4. import _reduce from "./internal/_reduce.js";
  5. import tail from "./tail.js";
  6. import identity from "./identity.js";
  7. /**
  8. * Performs left-to-right function composition using transforming function. The first function may have
  9. * any arity; the remaining functions must be unary.
  10. *
  11. * **Note:** The result of pipeWith is not automatically curried. Transforming function is not used on the
  12. * first argument.
  13. *
  14. * @func
  15. * @memberOf R
  16. * @since v0.26.0
  17. * @category Function
  18. * @sig ((* -> *), [((a, b, ..., n) -> o), (o -> p), ..., (x -> y), (y -> z)]) -> ((a, b, ..., n) -> z)
  19. * @param {Function} transformer The transforming function
  20. * @param {Array} functions The functions to pipe
  21. * @return {Function}
  22. * @see R.composeWith, R.pipe
  23. * @example
  24. *
  25. * const pipeWhileNotNil = R.pipeWith((f, res) => R.isNil(res) ? res : f(res));
  26. * const f = pipeWhileNotNil([Math.pow, R.negate, R.inc])
  27. *
  28. * f(3, 4); // -(3^4) + 1
  29. * @symb R.pipeWith(f)([g, h, i])(...args) = f(i, f(h, g(...args)))
  30. */
  31. var pipeWith =
  32. /*#__PURE__*/
  33. _curry2(function pipeWith(xf, list) {
  34. if (list.length <= 0) {
  35. return identity;
  36. }
  37. var headList = head(list);
  38. var tailList = tail(list);
  39. return _arity(headList.length, function () {
  40. return _reduce(function (result, f) {
  41. return xf.call(this, f, result);
  42. }, headList.apply(this, arguments), tailList);
  43. });
  44. });
  45. export default pipeWith;