Enter the addicted Functional Programming world

The first steps to understanding the concept of Functional Programming are often the hardest and most important step, and with the right concept, you will save countless hours.

Learn how to drive

When learning how to drive, everyone has difficulties. Of course, it is easy to look at other people, but it is a different story when jumping on your own.

We practiced driving our parents, circling around the house, and really only ran out to the road when we were a little mature.

But thanks to that persistent practice, along with the "howling" moments in the car, we finally passed the exam to get a degree.

With a driver's license on hand, we go on ice all the way. With each move, we learn more interesting things and become more confident. And then one day, we have to drive a remote from another person, or when our car "goes to the altar" and has to buy a new one.

How does it feel to drive another car for the first time? Is it the same as the first frequency of driving in life? Other very different. For the first time, everything was strange. We were already in the car before, but only passengers. This time, we are the driver. People take all control.

But with this second car, we just wonder in the very simple questions like, where to put the key, where the light is, how to use the turn signal, or how to adjust the mirror rearview.

After that, everything was as smooth as before. But why is it easier than the next time?

That's because the new car is no different from the old one, also with the basic things needed in a car, and is almost placed in the same position.

The difference appears very little, and may be a few extra features, but we don't need them at the first few rides. Gradually, we all know the new features (or at least the features we really care about).

Well, learning programming languages ​​is not much different. The first time is the hardest. But when you are already proficient, the third second language becomes easier.

When you switch to a second language, you often ask yourself questions like, "How to create modules? How to find the array? What parameter is the substring function? "

You are confident that you can learn how to use this new language because it evokes the old language, maybe with some new things in the hope of helping you more effectively.

First ship

Whether you run one or dozens of cars in your life, imagine you are going to have to pilot a spaceship to try it out.

If you are going to fly a spaceship, don't expect the driving skills to help out so much. You will have to come back from zero.

You will start practicing with the idea that everything in space will be very different from the ground. Physics has not changed, the only change is the way you travel in the same universe.

Similarly with Functional Programming, you need to know that everything will be very different. And what you previously knew about programming would not be applicable to this new field.

Programming is thinking, and Functional Programming will teach you how to think differently. So different that you can't think the same way.

Forget everything you already know

Learning functional programming is not quite a babbling practice. Obviously, there are many similar concepts but it is better to be prepared to re-learn everything.

With the right view, you have more accurate expectations. And when there is an accurate expectation, you will not ignore it when knowledge becomes "difficult".

There will be a lot of things you, as a programmer, do that you can't move to functional programming.

Like in a car, you often back off to drive out of the car. But the spacecraft is not running backwards. You will become panic "WHAT IS WHAT? DON'T RUNNESS IT ?! DO NOT RUNNING WHAT WOULD YOU GO TO THAT ?! ”

As it turned out, the spaceship did not need to run back because of its special ability to move in three-dimensional space. When you start to realize this, suddenly running back or not is not important anymore. In fact, one day you will look back and realize how cramped the car really is.

Let's escape from the world of cold Command Programming world and jump into the warming Programming Ham stream.

Following this, we will introduce you to some of the most important concepts in Functional Programming that you need to grasp before you enter this strange world.

Purity

When programmers specialize in Functional Programming talk about Purity, they are referring to Pure functions (pure function).

Ham Thuan are very simple functions. They only work on input parameters.

The following is an example of the Pure Function in JavaScript:

Notice that the add function does not touch the variable z ; Do not read from z and also do not write to z . This function reads only x and y (its input) and returns two variables together.

It is a pure function. If the add function succeeds z , this function is no longer "pure".

Here is another example function:

In this function, justTen , is a pure function, so only the constant can be paid. Why so?

Because we don't provide any input. Thus, to be "pure", the function cannot access any information other than its input, and the only result that can be returned is a constant.

Because pure functions without parameters do not do anything, so very little is applied. If justTen is determined to be a constant it will be better.

Most useful functions must accept at least one parameter.

See the following function:

You see this function does not return any results. It adds x and y and puts it into variable z but doesn't return the result.

This is the pure function because this function only works with its input. The function has plus, but doesn't return the result, so it's pretty useless.

All useful Hams must return results.

Let's review the first add function above:

Notice that add (1, 2) will always be 3 . Not too surprising but only because of this pure function. If the add function uses some external value, you can never predict its behavior.

The Pure function will always generate the same output when specified with the same input.

Since Pure Function cannot change any external variables, all the following functions are impure (implicit):

All of the above functions all have in common is Side Effects (side effect). When you call these functions, they change the file and database table, send the data to the server or the OS call to get the socket. Not only do they stop manipulating operations on their input and returning the output, so you can't predict what these functions will result in.

Ham Thuan No side effects

In the Command Programming Languages ​​like JavaScript, Java, and C #, you'll come across Side Effects everywhere. Since then, debugging becomes more difficult because a knowledge can be changed anywhere in the program. So when you encounter a bug because the value of a variable is changed to the wrong value at the wrong time, where do you look? Everywhere? It's not OK.

At this time, sure you are thinking, "THE ONLY THEN DO Ham Thuan THE HELL DO THIS ?!"

In Functional Programming, you don't just write Pure functions.

Functional Languages ​​cannot remove Side Effects, but only limited. Because the program must interact with the real world, some part of each application must impure. The goal of functional programming is to minimize the amount of impure code and separate them from the rest of the program.

Invariant

Do you ask when you first see this code:

And teachers, whoever they are, will tell you to forget what you learned in math class, right? Because x can never be equal to x + 1 .

But in command programming, this code will receive the current value of x by 1 and place the result back to x . As for functional programming, x = x + 1 will not be accepted.

There are no variables in making a situation .

Stored values ​​are still called variables (because of history) but they are constant (eg, when x receives a value, the value will be permanently there).

Don't worry, x is often a local variable and has a fairly short life cycle. But as long as it is alive, it will never change.

The following is an example of a constant variable in Elm (Pure Programming Language for Web Programming):

If you are not familiar with ML-Style syntax, let me explain. addOneToSum is a function that takes two parameters, y and z .

In block let , x is associated with value 1 . Example: equals 1 for the end of life. Its lifecycle will end when the exit function, or more accurately, when block let be eveluate.

In the print block, the operation can include the specified values ​​in block let , viz. x . the result of the calculation x + y + z is returned; or more accurately, 1 + y + z is returned because x = 1 .

Once again, you may wonder, "THERE IS NO PROBLEMS HERE!"

So in which case do we want to adjust the variable? There are two cases: changing multiple values ​​(such as changing a single value of object and record) and changing the value of a value (such as a repeating counter).

Function Programming works with changes to values ​​in a record by making copies of records with changed values. Since it is not necessary to copy all parts of the record (through special data structures) this method still proved to be extremely effective.

Programmable solver functions change single value in the same direction, create copy.

And of course there is no loop.

"NO VERSION, DO NOT RETURN TO YOU ?!"

Hold on. Without a loop, it doesn't mean we can't loop, it's just that we won't have specific repeats like for , while , due , repeat , … only.

Repeat The function uses recursion to iterate.

In JavaScript, you have two ways to iterate:

You see, recursion (function method) has the same result as the for loop by calling itself with a new start ( start + 1 ) and the new container ( acc + start ). The recursion above does not modify the old values, instead using the new values ​​calculated from the old value.

Unfortunately, even if you spend some time researching, this is still a difficult point in JavaScript for two reasons. First, JavaScript syntax is quite confusing, and two, you may not be familiar with recursive thought errors.

In Elm, recursion is easier to read, and therefore, easier to understand:

The above command runs as follows:

You must be thinking that the repeat for easier to understand. Whether it is true or false, or just because of familiarity , the loop does not recursively need mutations, and mutations are not good.

I still can't fully explain the advantages of Immutability here, but you can read the Global Mutable State section in Why Programmers Need Limits to learn more.

One obvious benefit is that if you have access to a value in your program, you have the right to read only, meaning that no one else will change that value, including yourself. So there will be no sudden mutations.

Moreover, if your program is multi-threaded, there will be no other ways that you can "break down" you. That value is immutable and if another thread wants to change, it will have to create a new value from the old value.

In the 90s, I wrote a Game Engine for Creature Crunch and the biggest bug was the problem. I wish I knew about the "invariant" that day. But then, confess I was more worried about the impact of the difference between the speed of 2x or 4x on the CD drive on game performance.

Immutability makes code simpler and safer.

Oh my head !!!!

This is probably enough for now. In the following articles, we will talk about the Higher-order Functions, Functional Composition, Currying, …

ITZone via medium

Share the news now