composeWith.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import _curry2 from "./internal/_curry2.js";
  2. import pipeWith from "./pipeWith.js";
  3. import reverse from "./reverse.js";
  4. /**
  5. * Performs right-to-left function composition using transforming function. The last function may have
  6. * any arity; the remaining functions must be unary. Unlike `compose`, functions are passed in an array.
  7. *
  8. * **Note:** The result of composeWith is not automatically curried. Transforming function is not used
  9. * on the last argument.
  10. *
  11. * @func
  12. * @memberOf R
  13. * @since v0.26.0
  14. * @category Function
  15. * @sig ((* -> *), [(y -> z), (x -> y), ..., (o -> p), ((a, b, ..., n) -> o)]) -> ((a, b, ..., n) -> z)
  16. * @param {Function} transformer The transforming function
  17. * @param {Array} functions The functions to compose
  18. * @return {Function}
  19. * @see R.compose, R.pipeWith
  20. * @example
  21. *
  22. * const composeWhileNotNil = R.composeWith((f, res) => R.isNil(res) ? res : f(res));
  23. *
  24. * composeWhileNotNil([R.inc, R.prop('age')])({age: 1}) //=> 2
  25. * composeWhileNotNil([R.inc, R.prop('age')])({}) //=> undefined
  26. *
  27. * @symb R.composeWith(f)([g, h, i])(...args) = f(g, f(h, i(...args)))
  28. */
  29. var composeWith =
  30. /*#__PURE__*/
  31. _curry2(function composeWith(xf, list) {
  32. return pipeWith.apply(this, [xf, reverse(list)]);
  33. });
  34. export default composeWith;