Currying is a concept in functional programming where a function is transformed into a sequence of functions that take one argument at a time.
Instead of writing a function that takes multiple arguments all at once, you break it down so that each function returns another function, which eventually returns the final result.
- Reusability: You can reuse partially applied functions.
- Function Composition: Currying helps in composing functions together.
- Customization: You can customize functions more flexibly.
function Addition(a, b, c) {
return a + b + c;
}
let res = Addition(2, 3, 4);
console.log(res); // Output: 9This function takes all three arguments at once.
function Addition(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}let res = Addition(2); // returns function(b)
let data = res(4); // returns function(c)
let data1 = data(7); // returns a + b + c
console.log(data1); // Output: 13let res = Addition(2)(4)(7);
console.log(res); // Output: 13✅ This is currying in action!
const userObj = {
name: "Aman",
age: 25,
};
function userInfo(obj) {
return function(infoType) {
return obj[infoType];
}
}
let res = userInfo(userObj);
console.log(res("name")); // Output: "Varun"
console.log(res("age")); // Output: 25Here, userInfo(userObj) returns a function which can be reused to fetch any property of that object dynamically.
Currying is not just limited to a fixed number of arguments — we can make it infinite!
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
}
}
}
console.log(add(4)(5)(7)()); // ❌ TypeError: add(...)(...)(...)(...) is not a functionThis only works for 3 arguments. We can't call it like add(4)(5)(7)(...).
function add(a) {
return function(b) {
if (b) return add(a + b);
return a;
}
}
console.log(add(4)(5)(7)(9)()); // Output: 25- Each function call keeps adding to
aand returns a new function. - When you finally call with no argument, it stops and returns the result.
If you want to call a function like this:
add(2)(4)(6)(8)(10)(); // -> 30Here’s a generic version:
function currySum(a) {
return function next(b) {
if (b !== undefined) {
return currySum(a + b);
} else {
return a;
}
}
}- Currying converts a function with multiple arguments into a chain of functions with single arguments.
- It allows for clean, reusable, and composable code.
- Use it for:
- Configurable logic
- Partial application
- Building DSL-like APIs
- And yes, you can even create infinitely curried functions that resolve when you stop supplying new arguments.
| Feature | Description |
|---|---|
| Currying | Breaking a function into smaller functions |
| Benefit | Reusability, readability, modularity |
| Real-life Use Case | Dynamic object property access |
| Infinite Currying | Allows dynamic number of arguments |
| Return Trigger | Use a check (like if (!b)) to return final value |
Try creating a curried function multiply(a)(b)(c) and greet(greeting)(name)!
Happy Coding! 😄