In JavaScript, partial application allows you to predefine some arguments of a function and use the resulting function later with additional arguments. This can make functions more reusable and easier to work with.
- Partial application fixes some arguments of a function in advance.
- The remaining arguments can be supplied later.
- Rest (...) and spread (...) syntax can help handle dynamic arguments.
- Partial functions are useful for creating specialized versions of general-purpose functions.
The following approaches can be used to work with partial functions in JavaScript.
Approach 1: Using a Higher-Order Function with the Spread Operator
A higher-order function can accept a function and some arguments, then return a new function that receives the remaining arguments.
function createPartial(func, ...fixedArgs) {
return function (...remainingArgs) {
return func(...fixedArgs, ...remainingArgs);
};
}
function add(a, b, c) {
return a + b + c;
}
const addTen = createPartial(add, 10);
console.log(addTen(20, 30));
Output
60
createPartial()receives the original function and the arguments to fix.fixedArgsstores the predefined arguments.- The returned function accepts the remaining arguments.
- The spread operator combines both sets of arguments before calling the original function.
Syntax:
function createPartial(func, ...fixedArgs) {
return function (...remainingArgs) {
return func(...fixedArgs, ...remainingArgs);
};
}
Approach 2: Using Rest Parameters to Handle Remaining Arguments
Rest parameters can be used to separate fixed parameters from the remaining arguments. The remaining arguments are collected into an array.
function processValues(first, second, ...remaining) {
console.log("First:", first);
console.log("Second:", second);
console.log("Remaining:", remaining);
}
processValues(1, 2, 3, 4, 5, 6);
Output
First: 1 Second: 2 Remaining: [ 3, 4, 5, 6 ]
- first receives the first argument.
- second receives the second argument.
- ...remaining collects all additional arguments.
- This approach is useful when a function needs to process a fixed number of arguments along with a variable number of additional arguments.
Syntax:
function functionName(arg1, arg2, ...remainingArgs) {
// function body
}
Approach 3: Creating a Reusable Partial Function
A partial function can be created once with predefined arguments and then reused with different remaining arguments.
function calculate(a, b, c) {
return a * b + c;
}
function partial(func, ...fixedArgs) {
return (...remainingArgs) =>
func(...fixedArgs, ...remainingArgs);
}
const multiplyByTwo = partial(calculate, 2);
console.log(multiplyByTwo(5, 3));
console.log(multiplyByTwo(10, 4));
Output
13 24
- partial() creates a new function with predefined arguments.
- 2 is fixed as the first argument of calculate().
- The remaining arguments are supplied when multiplyByTwo() is called.
- The same partially applied function can be reused with different values.
Note: Partial application and currying are related concepts but are not exactly the same. Partial application fixes one or more arguments in advance, while currying transforms a function with multiple arguments into a sequence of single-argument functions.