curry.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var createCurry = require('../internal/createCurry');
  2. /** Used to compose bitmasks for wrapper metadata. */
  3. var CURRY_FLAG = 8;
  4. /**
  5. * Creates a function that accepts one or more arguments of `func` that when
  6. * called either invokes `func` returning its result, if all `func` arguments
  7. * have been provided, or returns a function that accepts one or more of the
  8. * remaining `func` arguments, and so on. The arity of `func` may be specified
  9. * if `func.length` is not sufficient.
  10. *
  11. * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
  12. * may be used as a placeholder for provided arguments.
  13. *
  14. * **Note:** This method does not set the "length" property of curried functions.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @category Function
  19. * @param {Function} func The function to curry.
  20. * @param {number} [arity=func.length] The arity of `func`.
  21. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  22. * @returns {Function} Returns the new curried function.
  23. * @example
  24. *
  25. * var abc = function(a, b, c) {
  26. * return [a, b, c];
  27. * };
  28. *
  29. * var curried = _.curry(abc);
  30. *
  31. * curried(1)(2)(3);
  32. * // => [1, 2, 3]
  33. *
  34. * curried(1, 2)(3);
  35. * // => [1, 2, 3]
  36. *
  37. * curried(1, 2, 3);
  38. * // => [1, 2, 3]
  39. *
  40. * // using placeholders
  41. * curried(1)(_, 3)(2);
  42. * // => [1, 2, 3]
  43. */
  44. var curry = createCurry(CURRY_FLAG);
  45. // Assign default placeholders.
  46. curry.placeholder = {};
  47. module.exports = curry;