TIL the difference between partial application and currying
POSTED ON:
TAGS: advanced javascript closure
Currying and Partial Application are related (because of closure), but they are of different concepts.
Partial application transforms a function into another function with smaller set of arguments.
// partial application
function add(a, b, c) {
return a + b + c;
}
function partial(fn, ...args) {
return function (...moreArgs) {
return fn(...args, ...moreArgs);
};
}
const addTwoNumbers = partial(add, 10, 20);
console.log(addTwoNumbers(30)); // Output: 60
Currying creates nesting functions according to the number of the arguments of the function. Each function receives an argument.
function add(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
const addThreeNumbers = add(10)(20);
console.log(addThreeNumbers(30)); // Output: 60
via JavaScript Currying Currying in JavaScript
Related TILs
Tagged: advanced