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.
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 Stack Overflow survey of 2017, the data are similar. Functional languages are by no means among the most popular. In the working world, more or less the same. We can see in Indeed that the number of jobs in functional languages is very low compared to other more popular languages like Java or C #.
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 one of those functional languages known as pure. This means that it does not allow the data to be mutated and that operations are treated as the evaluation of mathematical functions.
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 currification.
If you want to test Haskell, you can do it from the same browser.
Erlang / Elixir
The community of Erlang is experiencing a resurgence, thanks to Elixir and that is why we must take it into account. Erlang was a language initially created for the management of telephone switchboards of Ericsson back in 1986. With this premise it turned out to be a functional language, designed for distributed and fault-tolerant applications . Erlang like Elixir is a dynamic typing language.
With the resurgence of functional programming, José Valim created Elixir. A language with a Ruby-based syntax, which runs on the Erlang virtual machine and is fully compatible with it (in fact you can use Erlang libraries in Elixir). In this way, Elixir can take advantage of the concurrent features of Erlang, with a syntax closer to many programmers. In fact many Ruby programmers are starting to adopt Elixir, given their performance. The arrival of programmers at Elixir, is bringing new sap to Erlang, reinforcing both communities.
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. Scala is probably one of the most famous. Although Scala supports all the typical features of Java OOP, it also has many functional features such as: anonymous functions, higher order functions, lazy evaluation, currification, pattern matching, tuples, etc.
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.
HN already Analyze, who told us about their experience with Scala.
Another functional language that runs on the Java virtual machine is Clojure. Clojure is a dialect of Lisp, so part of the idea of representing code and data in the same way. The typing of Clojure, like that of other functional languages like Elixir is dynamic. And as in other functional languages Clojure promotes the immutability of the data. Clojure makes use of the concept of identity, which is something like a logical entity associated with different values over time.
If we want to try this language we can do it directly from the browser
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 from a browser
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.