Arity and Curry Depth
You'll learn: how the number of parameters a function accepts relates to how deeply it can be curried.
When working with curried functions, two concepts help you understand their structure: arity and curry depth.
What Is Arity?
Arity is simply the number of parameters a function expects. A function with one parameter has arity 1, two parameters has arity 2, and so on. In JavaScript, you can check arity using the length property:
const add = (a, b) => a + b;
console.log(add.length); // 2 (arity of 2)
const greet = (name) => `Hello, ${name}`;
console.log(greet.length); // 1 (arity of 1)
What Is Curry Depth?
Curry depth refers to how many nested function calls are needed before you get the final result. When you curry a function with arity 3, the curry depth is also 3—you'll call it three times:
const curriedSum = (a) => (b) => (c) => a + b + c;
// Curry depth of 3: call it 3 times
const result = curriedSum(1)(2)(3); // 6
The Relationship
Arity determines the maximum curry depth. A function with arity 4 can be curried to depth 4. Your curry helper function uses func.length to know how many arguments to collect before executing:
const curry = (func) => {
return function curried(...args) {
if (args.length >= func.length) { // Check arity
return func(...args);
}
return (...moreArgs) => curried(...args, ...moreArgs);
};
};
Key Takeaway: Arity (parameter count) dictates curry depth (nesting level); understanding both helps you design and debug curried functions effectively.