Lodash _.curry() method
What you’ll learn
- How
_.curry(func, arity)builds intermediate functions until enough arguments arrive. - Familiar call shapes from the Lodash docs: chained calls, grouped arguments, and placeholders.
- When to supply explicit
arityversus relying onfunc.length. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Helpful background: _.bind() / _.bindKey() for placeholders; Function hub for category context.
- Arity: how many positional parameters a function expects before it can run meaningfully.
- Higher-order functions: functions that return functions capturing pending arguments.
Overview
_.curry(func, arity) wraps func so arguments can be supplied one call at a time—or merged across calls—until the configured arity is satisfied, then func executes with the accumulated parameters. Placeholders mark “skip this slot for now,” matching the official Lodash examples.
Incremental args
Shape APIs that receive configuration in stages without rewriting func.
Placeholders
Reorder partial applications with curry.placeholder.
Tree-shakeable
Import lodash/curry when the rest of Lodash is unnecessary.
Syntax
_.curry(func, [arity = func.length], [guard]) - func: the function to curry; must be callable.
- arity: optional cap on required arguments when
func.lengthis wrong or zero. - guard: internal iteratee hook—when truthy Lodash ignores the supplied arity (see source
arity = guard ? undefined : arity). - Returns: a curried wrapper; official docs note
lengthis not mirrored like native functions.
Classic chained calls
Each segment passes one more argument until all three slots fill—identical outcome to passing everything at once.
import curry from "lodash/curry";
const abc = function (a, b, c) {
return [a, b, c];
};
const curried = curry(abc);
curried(1)(2)(3);
// => [1, 2, 3]
curried(1, 2, 3);
// => [1, 2, 3] Grouped intermediate arguments
Lodash lets you batch arguments across calls—here two arguments arrive together before the final third.
import curry from "lodash/curry";
const abc = function (a, b, c) {
return [a, b, c];
};
const curried = curry(abc);
curried(1, 2)(3);
// => [1, 2, 3] Curried placeholders
Official pattern: pin a and c, circle back to supply b last.
import curry from "lodash/curry";
const abc = function (a, b, c) {
return [a, b, c];
};
const curried = curry(abc);
curried(1)(curry.placeholder, 3)(2);
// => [1, 2, 3] 📋 _.curry vs _.partial
| Topic | _.curry | _.partial |
|---|---|---|
| Goal | Split arity into sequential calls | Fix chosen arguments once |
| Order discipline | Left-to-right slots until filled | You choose which positions are fixed |
| Placeholder | curry.placeholder | partial.placeholder |
Right-to-left currying is _.curryRight.
Pitfalls to avoid
func.lengthArity surprises
Default parameters, rest params, or builds stripping metadata yield wrong counts—pass arity explicitly.
arguments soup
Curry counts fixed slots; functions that ignore arity and read arguments need careful design.
Guard behavior
When Lodash invokes curry as an iteratee helper it enables guard semantics—normal application omits the guard.
❓ FAQ
Summary
- Purpose:
_.curry(func, arity)delays invocation until enough ordered arguments accumulate. - Contrast:
_.partialfixes specific slots;_.curryRightfills from the right. - Next: Lodash _.curryRight(), _.bindKey(), or official Lodash docs for _.curry.
Under the hood curry calls createWrap with WRAP_CURRY_FLAG; the optional guard argument exists so Lodash can treat _.map(collection, _.curry(fn)) correctly by dropping the iteratee’s arity hint.
6 people found this page helpful
