Currying Nowadays - Inheritance - Javascript

//English

Hi Guys! sorry I've been too busy at work. I will do my best to keep writing about how cool JS is and the things you can do ;)

Now, as you could see on my previous post, currying has evolved due to Function.prototype.bind. The purpose of this post is to see another example that's practical:

"Inheritance" using currying!

Imagine this scenario: The world has countries, a country has people. people could either be female or male and each person has a name, in JS, you could write a function like the following:


function person(country,sex, name){
    return 'Hello my Name is ' + name + ', I am a ' + sex + ' and I am from ' + country;
}

It looks easy but It will be hard to read and to maintain:


function person(country,sex, name){
    return 'Hello my Name is ' + name + ', I am a ' + sex + ' and I am from ' + country;
}

var pedro = person('Mexico','Male','Pedro');//Hello my Name is Pedro, I am a Male and I am from Mexico

var julian = person('Brazil','Male','Julian');//Hello my Name is Julian, I am a Male and I am from Brazil

var rosa = person('Mexico','Female','Rosa');//Hello my Name is Rosa, I am a Female and I am from Mexico

var emily = person('US','Female','Emily');//Hello my Name is Emily, I am a Female and I am from US

var rose = person('England','Female','Rose');//Hello my Name is Rose, I am a Female and I am from England

You need to repeat a lot of code, so let's take advantage of Currying:


function person(country,sex, name){
    return 'Hello my Name is ' + name + ', I am a ' + sex + ' and I am from ' + country;
}

/*
The variable 'mexico' will be a function similar to this:
function mexico(sex, name){
    return 'Hello my Name is ' + name + ', I am a ' + sex + ' and I am from Mexico';
}
*/

var mexico = person.bind(null,'Mexico');

/*
The variable 'mexicoMale' will be a function similar to this:
function mexicoMale(name){
    return 'Hello my Name is ' + name + ', I am a Male and I am from Mexico';
}
*/

var mexicoMale = mexico.bind(null,'Male');

/*
The variable 'mexicoFemale' will be a function similar to this:
function mexicoMale(name){
    return 'Hello my Name is ' + name + ', I am a Female and I am from Mexico';
}
*/

var mexicoFemale = mexico.bind(null,'Female');

/*
Now in order to print Pedro, we just call the function 'mexicoMale'
The output will be exactly the same as the one Before:

Hello my Name is Pedro, I am a Male and I am from Mexico
*/

var pedro = mexicoMale('Pedro');

It looks like you have more code. The truth is that you have more REUSABLE CODE! The function person looks nice, but each time you call it, you need to repeat code, however, the function mexico, can be used each time you want to create a new person from Mexico! we don't need to repeat code, the function maleMexico and femaleMexico can be used to declare either a male or female person from Mexico and the code is more readable and the best thing: The function person has not been modified! you can use it as before.

NOTE : As you remember, the first param passed when you used Function.prototype.bind is the context, for this example, it isn't needed because we are not using this keyword; if we're using it, then the context is important.

Following this technique you can break your functions logically and still have your original function for legacy support or for other reasons. Just keep in mind the order of your params, this is important.

<!-- Example -->


See this running example to see it working :)

<!-- Summary -->


Well guys, this example helped us to mimic the Inheritance, but the truth is we are not doing Inheritance, we are just splitting complex functions into smaller and reusable functions.

Currying helps us do that, to split complex functions into small functions, moreover with bind we are splitting complex functions in memory! the original functions remain the same, but in memory we have more reusable functions!

So, that's it for today, I hope this example has been useful. If you have any questions or concerns you can reach me through twitter or leave your comments here :) See you!

Comments

Popular posts from this blog

Juego de Gato Usando HTML, JavaScript y CSS

AfterEffects - Quitar el Fondo de un Video Forma 1: KeyLight

Crear un nuevo Libro de Excel con VBA ES