Embracing Uncertainty In Your Writing Journey

In order to embrace uncertainty and ambiguity as you try to become a writer, you must develop an entrepreneurial mindset. In this article, I will give you specific tips on how to shift your thinking…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to quickly understand currying in JavaScript

In this article I’ll quickly introduce you to currying, it’s purpose, and show you how to understand it with plain and simple code.

Currying is the process of converting a function of multiple arguments to a chain of functions of one argument. Like so:

But it’s not comfortable to do those manipulations manually every time we need to break down our functions througn currying. So we make curry function that can do it for us.

Curried functions are easy to partially apply, and therefore easy to reuse in slightly different scenarios. Imagine we need to create a user like in the example above. Ok, that’s easy, we already made a function for it. But now let’s make it more real.

Cool, we can create users. But what if we want to create a manager user in several places of our app? We can just call createUser(“manager”, …) every time. But what if that name will need to be changed later for technical reasons? Say, we’ll need to merge our database with a database of some other company, and they use numbers for roles? Search for every call to createUser and replace the first argument? Seems kinda wrong.

So we foresaw that scenario and made this function:

Feels wrong again, right? Redundant and verbose, because we duplicate our arguments for no reason. Imagine if we’ll need more arguments or something like createManagerUserWithCompanyAvatar(). We’ll need to copy those arguments all other every time.

How do we fix this? Easy! We curry our basic function (createUser) and partially…

Add a comment

Related posts:

5 Appealing Traits of a Man That Often Unappreciated

He was my stepdad. No longer now as he died 2 years ago from lung cancer. He worked really hard all his life. Making a living here in the village is hard as hell yet I never saw him complaining. He…

Android app DAO PplayMarket.io 2.0 and user account creation

To take full advantage of the decentralized platform DAO PlayMarket.io 2.0, from a mobile phone, based on the Android operating system, download the application from the site, at the link…

A New Study Linking Autism And Vaccines Reportedly Used Fake Data

A recent study that found a direct link between vaccines and autism in mice will soon be retracted after scientists reviewed the research and found errors in the design, methodology, analysis and…