Day 3: What is function πŸ› currying?

Bhushan Gaikwad
4 min readMay 31, 2021
😱 Are you hungry? hehehe

A bit!! hehe What is this function currying?

Do you know, What is bind and closure in Javascript?

No πŸ˜•

So let's understand bind first with simple example.
In above example, You have 2 objects `john` and `jane` and 1 function `printName`.Notice on line 2 we are logging `this.name` to the console.Now what happens when we call `printName` function?
We have an empty output, It's because Javascript doesn't know what `this` means inside `printName` function and console Web API treats it as an empty string. So you get empty output.Bind function helps you teach `printName` function what should be `this` inside `printName` function and it returns copy of that function, so you can invoke it later like below.
In Line 9, copy of `printName` function is stored in `copyOfPrintNameForJohn` variable where `this` means `john` object.so line 7 prints `copyOfPrintNameForJohn` function in console and then on line 6 we invoke this function and see "John".Same thing happens for Jane. In short, with bind function you can teach javascript to use which object as `this` inside that function.

Oh!! but what if I have to pass parameters?

In the bind function after first parameter you can pass those parameters as comma separated values like in line 2.

Okay…. What is closure now?

If you have function inside a function, that inner function is closure. Thats it!Side-note: Many people get confuse between closure and callback.```easy-peasy-stuff
Inner function is called closure.
Function passed as parameter is called callback.
```
Thaaaaaaatssss it!!

Hey, Thanx, those two lines are pretty easy to remember 😎.

Okay, As you know what is bind and closure, Now let's move to the original question. Let's make πŸ› curry πŸ˜‚ 🀣.

sure πŸ˜‚ 🀣

First of all, Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).

Went over the head !!

Okay, Let's take an example.

Output:

Let's run this program,Line 12 to 8, We have a log function which accepts 3 parameters, Date object, Log importance string and Actual message string.so obviously to run this function we need to pass all three parameters ie. line 7, Which logs the first output into the console.Now on line 5, We bind log function and pass the second argument as Date Object (Current date and time). So as we know already this second param will be passed to a `log` function as first parameter and new copy of that function will be returned, which we store as `logNow`.Now, We can use this function anywhere inside our project to log anything with current time.In the line 4, We do the same thing, We only pass Log importance and Actual Message because `logNow` already has `new Date()` passed as first parameter.This is called Currying.

πŸ₯΅ Really?

Yep!! Check second output.Now, Let's make it further more useful.On line 2, We now bind `logNow` function and pass second parameter as `DEBUG`. At this moment that bind function should again return new copy of function which we store as `debugNow`.At this moment, `debugNow` already has `new Date` and `DEBUG` as parameters set, so we only need to pass actual message.and there you see last console log.Easy Peasy Right πŸ˜…

πŸ˜… Kind of, Let me check it once, but example is nice, I was not able to find such a real life example.

Honestly, I spent an hour thinking about right example 😬.

I like it 😎, Now what about closures? You discussed that as well!!

Currying could be achieved using closures as well, but It's easy.It could be homework for you 😜.

Okay fair enough!

πŸ‘‹ See you tomorrow.

πŸ‘‹

and yes.....

Yeah, I did joined you on Twitter, LinkedIn and Github.

I know you share lot of cool tips and tricks regarding PHP, Laravel, Javascript, Vuejs, Nuxtjs, Reactjs, databases, workflows, productivity etc etc.

https://twitter.com/rckstrbhushan

https://www.linkedin.com/in/rckstrbhushan

https://github.com/bhushan

πŸ‘†πŸ»πŸ‘†πŸ»πŸ‘†πŸ»πŸ‘†πŸ»πŸ‘†πŸ»πŸ‘†πŸ»πŸ‘†πŸ»

Take care and stay safe. Cheers πŸ₯‚

🀣🀣🀣🀣🀣🀣 Thank You. See you tomorrow with another question.

--

--