You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.8 KiB

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