Day 3: What is function π currying?
π± 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
ππ»ππ»ππ»ππ»ππ»ππ»ππ»
Take care and stay safe. Cheers π₯
π€£π€£π€£π€£π€£π€£ Thank You. See you tomorrow with another question.