Today I Learned - Rocky Kev

TIL the difference between partial application and currying

POSTED ON:

TAGS:

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:

TIL not using try/catch block with promises for error catching

You shouldn't use a try/catch block inside the Promise Definition. Your promise is already doing that.

TIL using proxies for reactivity

A Proxy let you detect when someone gets, sets, or updates a property on an object, and run code when they do. But why? This usecase -- when you add/update the object, it also updates localStorage!

TIL the difference between partial application and currying

Partial application transforms a function into another function with smaller set of arguments. Currying creates nesting functions according to the number of the arguments of the function. Each function receives an argument.