The resurgence of functional programming
Even if you’ve never worked with her, you’ve probably heard of functional programming. And every day more, since there seems to be a hype with it. This paradigm of programming, which seemed to have been forgotten by the great mass of developers, has been reviving for a few years.
It is true that other imperative languages, especially imperatives and object oriented, have been known to be more productive and effective for a greater number of tasks. But not everyone thinks the same. Recently a critical current has appeared, arguing that most of the advantages that this type of languages propose, may not be so definitive. While the POO has always promised to model the real world and provide reusable code, when we gain some experience we realize that is not simple.
So it has not been slow to appear people who think it is easier to program with a functional approach, even claiming that it is much more productive and that reduces the number of bugs and errors.
If functional languages are the solution to all our problems (surely not), it is something that time will tell, but in the meantime we can analyze what options we have.
Table of Contents
Why the resurgence of functional programming?
Functional programming has been around for many years, but it has always been associated with academic rather than business and productive environments. It is something that seems to start to change and many companies, some very important, are betting strongly on this paradigm. Why?
One of the reasons may be that applications are becoming increasingly difficult to run on a single machine. It is increasingly necessary to be able to withstand large doses of concurrency and parallelism. Notwithstanding the current hardware , it provides us with unparalleled parallelism capabilities, and the cloud makes it easier and cheaper to build distributed systems on different servers. And of course, for tasks like these, a functional language can develop like a fish in water.
Functional programming has very interesting concepts, so much so that many non-functional languages are gradually adopting them
It is also clear that functional programming has very interesting concepts, so much so that many non-functional languages are slowly adopting them. The latest versions of Java already include lambda expressions , something already included C # a long time ago. Each version of C # includes more functional features and in fact in its latest version, the 7, includes the functional concept of pattern matching. Ruby, Python or Go, are other languages that incorporate some functional characteristics.
But this does not end here, since the old functional rockers, are adding many new languages, with many other modern features. This is the case of Elixir, Clojure, Scala or Elm, to name a few.
But is this noise or is it a real trend?
Programmers love being up to speed. Before we learn Cobol, we may prefer to learn something more modern (which does not have to be better). So much we like to learn, that many times we end up falling into the hype without stopping to think about the consequences.
Thinking about the cold data, and looking at the last month’s Tiobe index, we can see that functional languages are very far from the top. There is more data to help us corroborate this point and if we go to the famous
This does not mean that we should leave functional programming aside, simply that we must put it in context. If we want to find a job as a programmer, it is easier to find a job using a nonfunctional language. But it is true that numerous startups, those who are changing the rules of the game, increasingly bet on that paradigm. So it may not be unreasonable to start investing time in learning any of these languages. And if not, at least we will have learned a lot by the way.
What options do I have if I want to learn functional programming?
If we want to learn functional programming, today we have many options. For all tastes and for all tastes. Here are some of them:
Haskell is based on lambda calculus (hence its logo) and bears its name on Haskell Brooks Curry, an American mathematician, who curiously names another programming language (Curry) and the concept of
If you want to test Haskell,
Erlang / Elixir
The community of
With the resurgence of functional programming, José Valim created
If you want more information, in Wesharethis, we have also talked about Elixir.
The Java virtual machine is one of the most advanced, and this is causing many new languages to be generated around it.
Obviously Scala can not be defined as pure functional language (it is rather multiparadigma), but even though it is an object-oriented language, it moves away from imperative programming.
The Scala code is compatible with existing programs in Java, which is always a plus.
Another functional language that runs on the Java virtual machine is
If we want to try this language
F # is Microsoft’s functional language, compatible with the .NET stack. By default the data are immutable, although we can specify that they can mutate their state. The F # syntax is based on OCaml, but with differences. One of the curiosities of F # is that it is a static language, in which you do not have to define types, since these are almost always inferred by the compiler.
F # is multiparadigma, so it has imperative programming touches like loops
forand also supports object-oriented programming.
If you want to try F # you can do it
Okay, there are many functional languages, but why should I use them?
Although it is true that programmers often live in fashions, it is also true that a bad tool rarely becomes popular. So if functional programming is getting more attention it will be for something . Here are some of the supposed advantages we can get:
- The code tends to be more concise and expressive
- That the state is immutable, avoids many mistakes because there are no side effects
- That the state is immutable, it helps us in concurrent or parallel systems
- Functions are given parameters and return a result, so they are always predictable (if they are pure functions).
- The testing tends to be easier, because if we write pure functions, the results are more predictable.
However, even if these advantages are not enough, although in our day to day use other types of languages, it is highly recommended to learn other programming paradigms. It opens the mind to new ideas, and that makes us better programmers. And it is that sometimes, it enriches much more to learn a new language, than to learn more things of the language that we use every day.