Why do we need to learn functional programming? Or what are the advantages of functional programming? In this series, I’ll use Scala to give you the advantages of functional programming, as well as some functional philosophies. It doesn’t matter if you don’t know Scala. Scala is similar to Java, and I’ll explain the Scala syntax used in this section at the beginning of each article.

Why is functional programming so popular these days

Like Python, Functional Programming (FP) has only been known in recent years, but it is not a new concept. It has a history almost as long as object-oriented programming (OOP). But in the context of everything, there’s always a reason, and what’s the reason functional programming has become so popular in recent years?

The main reason is the gradual failure of Moore’s Law, the development of computers tend to multi-core CPU and distributed direction. We often use the advantage of object oriented programming is able to well for areas to solve the problem of modeling, but its synchronous blocking calls in a multithreaded programming environment, and the resulting thread safety, natural and functional programming is suitable for distributed concurrent programming programming way, really. The future is clearly the era of big data, so functional programming will only become more important, and it may even be the future of functional programming.

Moore’s Law: In 1965, Intel founder Gordon Moore proposed that the integration of integrated circuits would double every two years for up to 10 years. Moore’s Law. The cycle was later reduced to 18 months. In other words, the performance of IT products such as computers doubles every 18 months. Or the price of IT products with the same performance, such as computers, will drop by half every 18 months. For decades the IT industry has moved at a pace predicted by Moore’s Law.

What is functional programming

There’s an interesting article that might give you an idea of functional programming, and you can read it first, and then you can read the formal definition of functional programming, the Functional Programming Bible.

Functional programming refers to writing code as pure functions, defined as follows:

Pure function: a function has no side effects in the execution of the program except that it gives the operation results according to the input parameters. We can call this kind of function pure function.

The core purpose of pure functions is to write code without side effects, and many of its features, including invariants, lazy evaluation, and so on, serve this purpose. So what does it mean to have no side effects? Let’s look at an example.

Coffee shop shopping example — Scala

Let’s take a look at some code with side effects:

 

What are the side effects of this function? When you buy a cup of coffee and charge it with a credit card, it notifies the credit card company to do a bunch of processing.

What’s the problem with that? First, the side effect is to make the code thread-unsafe. Second, it makes the code hard to test, and if you wanted to test the logic of the code, you’d have to charge your credit card every time. But we just want to test the logic, we don’t really want to deduct the fee. Also, when you want to buy more than one cup of coffee at a time, you have to skip.

  


Now let’s look at the functional way to implement:

See, after we’ve changed it this way, the function has no side effects. That is, no matter how many times I execute the buyCoffee function, it will only return me a cup of coffee and its price, so that we can easily test its logic without worrying about credit cards. And it can safely run in a multithreaded environment.

In fact, from the object oriented point of view, is this a little like some design patterns in object oriented? This decouples the relationship between coffee and credit cards, making it easier to combine them later when adding other features, such as charging for multiple cups of coffee, that would be a pain to implement. But after a functional adaptation, some things become clear

In this sense, functional programming can also be a way of thinking about programming. It won’t get you a better job immediately, but it can somehow change your thinking and make you write better code.

conclusion

A lot of new concepts have caught on in recent years, but they were invented in the last century, whether it’s machine learning, deep learning, Python, or functional programming. Why is that? That’s because the technological boundaries have changed, or the technological boundaries of this era have changed.

Every age has its technological boundaries. True engineers know where the boundaries lie, but only amateurs can’t. Buffett says he doesn’t invest in things he doesn’t understand precisely because he sets boundaries.

One of the most important reasons for Apple’s success is that it clearly understands the boundaries of its time and can do its best within those boundaries. You see, many of Apple’s products are epoch-making, but in fact, they were not invented by Apple. For example, the smart phone was first invented by DOCOMO, a Japanese company, and the personal tablet computer was first invented by The UK. The IPod and MP3 were also first produced in Korea. Many of the technologies Apple uses were even 30 years old, but why didn’t anyone know about them until they were invented?

It is precisely because Apple understands the technological boundaries of its time and does its best within them.

To put it more simply, when we are learning a new technology, or using it to do a job, we have to see where the boundaries are. More broadly, like Apple, we should be thinking about where the technological boundaries of our era lie so that we don’t get caught up in an endless technological catch-up.