Introduction
Partial application fixes some arguments of a function, producing a new function with fewer parameters. Unlike currying, it can fix any number of arguments at once.
Key Concepts
Partial Application: Fixing some arguments of a function upfront, returning a new function that takes the remaining arguments.
bind(): JavaScript's built-in partial application: fn.bind(null, arg1, arg2).
Partial from Right: Fixing the last arguments instead of the first—useful when the data comes first.
Real World Context
Event handlers often use partial application: onClick={handleDelete.bind(null, itemId)}. API client methods partially apply the base URL: const getUser = get.bind(null, '/api/users'). Configuration functions that fix options while leaving data flexible are everywhere in production code.
Deep Dive
Using bind()
javascriptfunction greet(greeting, name) { return `${greeting}, ${name}!`; } const sayHello = greet.bind(null, 'Hello'); sayHello('Alice'); // 'Hello, Alice!' sayHello('Bob'); // 'Hello, Bob!'
Partial Function
javascriptfunction partial(fn, ...presetArgs) { return function(...laterArgs) { return fn(...presetArgs, ...laterArgs); }; } const sayHello = partial(greet, 'Hello'); sayHello('Alice'); // 'Hello, Alice!' // Multiple preset args const add = (a, b, c) => a + b + c; const add5and10 = partial(add, 5, 10); add5and10(15); // 30
Partial from Right
javascriptfunction partialRight(fn, ...presetArgs) { return function(...laterArgs) { return fn(...laterArgs, ...presetArgs); }; } const divide = (a, b) => a / b; const divideBy2 = partialRight(divide, 2); divideBy2(10); // 5
Currying vs Partial Application
javascript// Currying: unary chain const curried = a => b => c => a + b + c; curried(1)(2)(3); // 6 // Partial: fix any number const partial = partial(add, 1, 2); partial(3); // 6
Common Pitfalls
- Confusing currying with partial application — Currying creates a chain of unary functions. Partial application fixes N arguments at once, regardless of arity.
- Losing
thiswith bind —bind(null, ...)setsthistonull. If the function usesthis, pass the correct context as the first argument. - Over-specializing — Partially applying too many arguments creates functions so specific they're only used once, adding complexity without reuse.
Best Practices
- Use
bindfor simple cases —fn.bind(null, fixedArg)is built-in and familiar to all JavaScript developers. - Write a custom
partialfor flexibility — Apartial(fn, ...presetArgs)utility is clearer thanbindfor non-trivial cases. - Combine with composition — Partially applied functions are perfect building blocks for
pipe()andcompose()pipelines.
Summary
Partial application fixes some arguments upfront. bind() for simple cases. Custom partial for flexibility. Different from currying in flexibility.
Code Examples
function partial(fn, ...presetArgs) {
return function(...laterArgs) {
return fn(...presetArgs, ...laterArgs);
};
}
function greet(greeting, name) {
return `${greeting}, ${name}!`;
}
const sayHello = partial(greet, 'Hello');
sayHello('Alice'); // 'Hello, Alice!'
sayHello('Bob'); // 'Hello, Bob!'
// Using bind (built-in partial application)
const sayHi = greet.bind(null, 'Hi');
sayHi('Charlie'); // 'Hi, Charlie!'