Currying and Partial Application are functional programming techniques that transform how functions receive their arguments — both help create reusable, modular, and customizable functions.
1️⃣ Currying
Currying transforms a function that takes multiple arguments into a series of functions, each taking one argument at a time.
Example (Normal Function)
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2, 3)); // 6
Curried Version
function curryAdd(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
console.log(curryAdd(1)(2)(3)); // 6
✅ Each function remembers the argument from its outer scope (closure) — that’s how currying works.
Simplified with Arrow Functions
const curryAdd = a => b => c => a + b + c;
console.log(curryAdd(2)(4)(6)); // 12
Why Currying Is Useful
- Creates reusable functions.
- Makes it easier to create customized versions of functions.
Example:
const multiply = a => b => a * b;
const double = multiply(2);
const triple = multiply(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15
2️⃣ Partial Application
Partial application means fixing some arguments of a function now and providing the rest later.
It differs from currying — it doesn’t require one argument per function call.
Example
function add(a, b, c) {
return a + b + c;
}
function partialAdd(a) {
return function (b, c) {
return add(a, b, c);
};
}
console.log(partialAdd(2)(3, 4)); // 9
✅ Here, partialAdd(2) fixes the first argument (a = 2) but still allows b and c to be passed together.
3️⃣ Currying vs Partial Application
| Feature | Currying | Partial Application |
|---|---|---|
| Arguments | Takes one at a time | Fixes some, passes the rest later |
| Function Calls | One per argument | One or more arguments per call |
| Example | f(a)(b)(c) |
f(a)(b, c) |
| Use Case | Function transformation | Pre-filling known arguments |
💡 In Short:
- Currying: Breaks a multi-arg function into a chain of single-arg functions.
- Partial Application: Pre-fills some arguments, allowing the rest later.
Both make your code more modular, reusable, and expressive.