Master Advanced Javascript: Function Currying and Partial Application for Code Reuse
Every one of us might have come across functions that accept a lot of parameters. It looks great while defining but using it is a nightmare. One more issue with this is that just to change one value in these parameters we’ve to rerun the function. It will cause performance bailouts when it comes to large-scale applications.
In this article let’s explore the concepts of currying and partial application to define powerful functions that help us in overcoming these issues.
Function Currying
Just Imagine function as a recipe to cook a sandwich. We need to add ingredients like tomatoes, onions, cheese, pepper,… If we add all these together, it’ll work.
// Method Declaration
function sandwichMaker(
tomatoes,
oninons,
cheese,
pickles,
mustard,
mayo,
keyIngredient
) {
return (
"We're cokking your sandwich: " +
tomatoes +
oninons +
lettuce +
cheese +
pickles +
mustard +
mayo +
keyIngredient
);
}
// Method call
console.log(sandwichMaker(true, true, true, true, true, true, true, "Panner"));
But what about organizing, efficiency, and reusability which will differentiate a master chef from a sue chef? Instead, Let’s use currying to make it more…