JavaScript Currying Magic!šāØ
Hi to all of you! Iām sure this topic will help you understand the delightful topic I have to share with you.
Today, weāre diving into the enchanting world of currying in JavaScript. But donāt worry, weāre going to learn it step by step, just like building the coolest LEGO castle. So, grab your imagination hats, and letās embark on this magical coding journey!
Simple Currying implementation in JavaScript
What is Currying?š„
Okay, imagine youāre making your favorite peanut butter and jelly sandwich. Usually, you grab all the ingredients at once and make your delicious snack. But what if we break it down into steps?
What's currying like?
Currying is like making your sandwich step by step. First, you spread peanut butter on one slice, then add jelly to another. Itās doing things in little parts!
The JavaScript MagicāØ
Now, letās bring this sandwich magic into JavaScript. In JavaScript, functions can be like recipes. Currying helps us break down these recipes into steps.
Why do we curry functions?
Currying is helpful because it makes our code more organised. Instead of doing everything all at once, we can break it into smaller tasks. Each step, or ānest,ā gets us closer to the final result.
Letās Code a Curried Functionš
Now, letās write a simple curried function. Imagine we have a function that adds three numbers:
function sum(a, b, c) {
return a + b + c;
}Now, letās curry it!
function curriedSum(a) {
return (b) => {
return (c) => {
return a + b + c;
};
};
}How does our curried function work?
Get Mukta Kumariās stories inĀ yourĀ inbox
Join Medium for free to get updates fromĀ thisĀ writer.
Our curried function works like a set of building blocks. You call it with one number, then another, and finally the last one. Each call returns a new function, waiting for the next number.
Behind the Scenes: How Does Currying Work?
Letās peek behind the curtain. When you curry a function, itās like unfolding a story. Each call to a curried function returns a new function, eagerly waiting for its moment in the spotlight.
let sum1 = sumCurried(1);
let sum2 = sum1(2);
let result = sum2(3);
console.log(result); // Result: 6Letās have some Fun!š
Now, letās play with our curried function:
const result = curriedSum(1)(2)(3);
console.log(result); // Output: 6Why is this fun?
Itās like playing with LEGO blocks! You build up the result step by step. One call for each number, just like adding one LEGO block at a time.
Exploring Currying Limitsš¤
Now, you might wonder if currying has limits. Well, yes, like all magical things, it has its boundaries.
Is there a limit to currying?
Currying can go as deep as you want, but it might become too complex if you nest at too many levels. Itās like stacking too many LEGO blocks ā it can get a bit tricky to manage.
A little more than currying
By definition, currying should convert sum(a, b, c) into sum(a)(b)(c).
But most implementations of currying in JavaScript are advanced, as described: they also keep the function callable in the multi-argument variant.
Summary
Currying is a transform that makes f(a,b,c) callable as f(a)(b)(c). JavaScript implementations usually both keep the function callable normally and return the partial if the arguments count is not enough.
Currying allows us to easily get partials. As weāve seen in the logging example, after currying the three argument universal function log(date, importance, message) gives us partials when called with one argument (like log(date)) or two arguments (like log(date, importance)).
and my little advise see this Akshay Sainiā¦vid you better understand the concept.
Thanks for reading, hope u better understand nowš.








