curryN.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import _arity from "./internal/_arity.js";
  2. import _curry1 from "./internal/_curry1.js";
  3. import _curry2 from "./internal/_curry2.js";
  4. import _curryN from "./internal/_curryN.js";
  5. /**
  6. * Returns a curried equivalent of the provided function, with the specified
  7. * arity. The curried function has two unusual capabilities. First, its
  8. * arguments needn't be provided one at a time. If `g` is `R.curryN(3, f)`, the
  9. * following are equivalent:
  10. *
  11. * - `g(1)(2)(3)`
  12. * - `g(1)(2, 3)`
  13. * - `g(1, 2)(3)`
  14. * - `g(1, 2, 3)`
  15. *
  16. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  17. * "gaps", allowing partial application of any combination of arguments,
  18. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  19. * the following are equivalent:
  20. *
  21. * - `g(1, 2, 3)`
  22. * - `g(_, 2, 3)(1)`
  23. * - `g(_, _, 3)(1)(2)`
  24. * - `g(_, _, 3)(1, 2)`
  25. * - `g(_, 2)(1)(3)`
  26. * - `g(_, 2)(1, 3)`
  27. * - `g(_, 2)(_, 3)(1)`
  28. *
  29. * @func
  30. * @memberOf R
  31. * @since v0.5.0
  32. * @category Function
  33. * @sig Number -> (* -> a) -> (* -> a)
  34. * @param {Number} length The arity for the returned function.
  35. * @param {Function} fn The function to curry.
  36. * @return {Function} A new, curried function.
  37. * @see R.curry
  38. * @example
  39. *
  40. * const sumArgs = (...args) => R.sum(args);
  41. *
  42. * const curriedAddFourNumbers = R.curryN(4, sumArgs);
  43. * const f = curriedAddFourNumbers(1, 2);
  44. * const g = f(3);
  45. * g(4); //=> 10
  46. */
  47. var curryN =
  48. /*#__PURE__*/
  49. _curry2(function curryN(length, fn) {
  50. if (length === 1) {
  51. return _curry1(fn);
  52. }
  53. return _arity(length, _curryN(length, [], fn));
  54. });
  55. export default curryN;