Function reusability with currying and partial applied in Javascript
Both partial application and currying are functional programming concepts that involve transforming functions to create new functions with specific behaviors. However, they achieve this in different ways:
Recommended by LinkedIn
function multiply(a, b)
return a * b;
}
function partial(func, ...args) {
return func(...args);
}
const double = partial(multiply, 2);
double(5); // Result: 10
double(20); // Result: 40
function multiply(a, b)
return a * b;
}
const currying = (func) => (arg1) => (arg2) => {
return func(arg1, arg2);
};
const double = currying(multiply)(2);
double(5); // Result: 10
double(20); // Result: 40
In summary, partial application fixes some arguments to create a new function, reducing the number of arguments, while currying transforms a function to take one argument at a time, returning new functions in a chain until all arguments are provided. Both concepts are powerful techniques in functional programming like javascript and can be used based on the specific requirements of your code.