curry.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import _curry1 from "./internal/_curry1.js";
  2. import curryN from "./curryN.js";
  3. /**
  4. * Returns a curried equivalent of the provided function. The curried function
  5. * has two unusual capabilities. First, its arguments needn't be provided one
  6. * at a time. If `f` is a ternary function and `g` is `R.curry(f)`, the
  7. * following are equivalent:
  8. *
  9. * - `g(1)(2)(3)`
  10. * - `g(1)(2, 3)`
  11. * - `g(1, 2)(3)`
  12. * - `g(1, 2, 3)`
  13. *
  14. * Secondly, the special placeholder value [`R.__`](#__) may be used to specify
  15. * "gaps", allowing partial application of any combination of arguments,
  16. * regardless of their positions. If `g` is as above and `_` is [`R.__`](#__),
  17. * the following are equivalent:
  18. *
  19. * - `g(1, 2, 3)`
  20. * - `g(_, 2, 3)(1)`
  21. * - `g(_, _, 3)(1)(2)`
  22. * - `g(_, _, 3)(1, 2)`
  23. * - `g(_, 2)(1)(3)`
  24. * - `g(_, 2)(1, 3)`
  25. * - `g(_, 2)(_, 3)(1)`
  26. *
  27. * Please note that default parameters don't count towards a [function arity](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length)
  28. * and therefore `curry` won't work well with those:
  29. *
  30. * ```
  31. * const h = R.curry((a, b, c = 2) => a + b + c);
  32. *
  33. * h(40);
  34. * //=> function (waits for `b`)
  35. *
  36. * h(39)(1);
  37. * //=> 42
  38. *
  39. * h(1)(2, 3);
  40. * //=> 6
  41. *
  42. * h(1)(2)(7);
  43. * //=> Error! (`3` is not a function!)
  44. * ```
  45. *
  46. * @func
  47. * @memberOf R
  48. * @since v0.1.0
  49. * @category Function
  50. * @sig (* -> a) -> (* -> a)
  51. * @param {Function} fn The function to curry.
  52. * @return {Function} A new, curried function.
  53. * @see R.curryN, R.partial
  54. * @example
  55. *
  56. * const addFourNumbers = (a, b, c, d) => a + b + c + d;
  57. *
  58. * const curriedAddFourNumbers = R.curry(addFourNumbers);
  59. * const f = curriedAddFourNumbers(1, 2);
  60. * const g = f(3);
  61. * g(4); //=> 10
  62. */
  63. var curry =
  64. /*#__PURE__*/
  65. _curry1(function curry(fn) {
  66. return curryN(fn.length, fn);
  67. });
  68. export default curry;