The Beauty of Randomness

I have a small mole smack dab in the middle of my right cheek. I never notice it since it’s always there, but I realize people recognize me for it, and it’s a great deterrent from committing a crime…

Smartphone

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




Function Programming With JavaScript Part 2

This article picks up where we left off in the previous article. If you do not know what functional Programming is please go through the previous article/story in this publication. In this article, we will be learning in-depth of what functional programming is.

Immutability simply means that you will not be able to change data once declared. In functional programming, data is immutable and never changes. For example, you want to share your resume with someone but don’t want to give them the whole information. In this case, you would probably make another copy of your resume and edit the copy. You would not make changes to main resume. Immutable data in application works quite similarly. Instead of changing the main data, we make changes to the copy version.

Let's take a look at how it works.

The first function that we defined updates the existing object. Imagine you worked on your assignment and your teacher told you to make some changes. But when you started working on the main copy, it somehow got corrupted. You will have to redo the whole assignment. If you had made a second copy of that assignment and started working on it, you would not have to face this issue. So, updating the existing data may cause issues like this.

On the other hand, the second function makes a copy of the person object and then mutates the copy. In this way, you do not need to worry about corrupting the existing data. You can also work with an array the same way.

A pure function is a type of function which has at least one argument and returns a value or argument. A pure function also does not mutate the argument it gets. It simply makes a copy and works on that copy. The second function in the previous example is a pure function as it full fills all the characteristics of a pure function.

Now you might be wondering how will data change in an application if data is immutable? To deal with this issue JavaScript has given us Data transformation. In functional programming, we will produce transformed copies of data and manipulate them. This saves the original copy of the data. You might be thinking why to go through all that trouble just to change data.

We need to go through all that just to make sure we do not mutate original data. For example, while we are inputting any data in the input field, data might will change with every keystroke. We don’t want to call the database with every keystroke, do we? We must make sure that we are calling the database as minimum as possible. For this reason, we first make a copy of the data then mutate. Some data transformation methods are as follows:

Higher-order functions are a kind of function that can take functions as an argument or return a function or both at the same time. For example, Array.map(), Array.filter(), Array.join() all are higher-order function as all of them take functions as argument. Another type of higher-order functions is those who return another function.

ow that we’ve been introduced to the core concepts of functional programming, let’s put those concepts to work for us and build a small JavaScript application. Since JavaScript will let you slip away from the functional paradigm, and you do not have to follow the rules, you will need to stay focused. Following these three simple
rules will help you stay on target.
1. Keep data immutable.
2. Keep functions pure — accept at least one argument, return data or another function.
3. Use recursion over looping (wherever possible)

When developing react application we should always try to follow these rules.

Add a comment

Related posts:

DETENIENEN A TRES SUJETOS POR ASALTAR UN OXXO

Durante la noche tres sujetos asaltaron un OXXO, entre las cosas que robaron en su mayoría fueron bebidas alcohólicas y una veladora. Los sujetos fueron identificados como “el yeah”, “el tocino” y…

Simplest Introduction To Machine Learning And Much More

TensorFlow is an open-source machine learning framework for everyone. As I believe, when we know the internals of a library, we can get the most out of it. And we feel much more confident while…

About Greenline

GREENLINE is a peer-to-peer electronic monetary system based on cryptography like Bitcoin yet unlike Bitcoin. It exhibits properties similar to physical currencies (such as dollars), how ever what…