Elixir is a functional programming language build on top of Erlang created by José Valim. He is well known for his contributions in the Ruby community and was part of the Ruby on Rails’ core team. He was struggling with multithreading and concurrency problems and was looking how various programming languages tackled the problem. Erlang stood out because of its concurrent nature but José found that there were things missing that could make the developer experience a lot better. And so Elixir was born.
It looks like ruby
The Elixir syntax is very Ruby-like so it’s easy to mistake the language for having the same semantics. However, don’t let the syntax fool you. After diving into Elixir you will noticed that it behaves entirely different than Ruby. Below is an example of a small Calculator component showing the similarity and some differences of the two languages.
# Ruby class Calculator do def add(x, y) do x + y end end calc = Calculator.new() calc.add(1, 2)
In the Ruby example we define what a Calculator looks like by defining its methods. We create an instance of this class and call the
add method to get the sum of the given arguments.
# Elixir defmodule Calculator do def add(x, y) do x + y end end # We don't create a Calculator instance Calculator.add(1, 2)
The Elixir example looks roughly the same except for two key differences. The first one being the
defmodule keyword. Elixir is a functional programming language so it doesn’t support classes. Instead the only thing that
defmodule does is grouping a few functions together for the developer’s convenience. This explains the second difference where we have to call the
add function on the
Calculator module without making an instance like in the Ruby example. The reason why Elixir doesn’t support classes is because it’s a concurrent language and in order to achieve that. It needs to handle state in a different manner than other popular languages.
Why learn Elixir?
State, concurrency and functional programming were words that I rarely used before diving into Elixir. What do they mean and why is this useful to know? As I understand, the problem with more traditional languages is that it’s very hard to run computations at the same time. For example if I have multiple cores available in my computer and I want to run 4 different calls of the previously mentioned
add function at the same time on the same
Calculator component. I am going to be having a bad time in Ruby. However, Elixir makes running these computations in parallel very trivial. Making it a great choice for creating an extremely fast calculator. Now there is a lot more to it, but for simplicity sakes. Remember that Elixir distributes these computations on our computer to be run at the same time. Making our application extremely fast.
In order to be concurrent. Everything in Elixir is immutable, meaning that no values can change. In fact there isn’t really an implementation for assigning values. Which to me, was very odd to reason about since I’ve seen the
= operator being used.
# looks like an assignment but isn't x = 1
= operator is called the match operator. Meaning that it doesn’t assign but matches. This was confusing since, in the example above, the operator is clearly used to assign the value 1 to
x. What happens is that the value 1 is being matched via a pattern to
x making the value of
x equal to 1. This is known as pattern matching and is a very powerful tool to have. This concept deserves to have an article all of its own so I won’t dive into the details.
Where to go now?
Elixir is a functional language that supports concurrent programming. This allows for creating extremely fast applications because computations are run at the same time. It requires a different way of thinking than in other more traditional languages, but in my opinion it’s well worth it.
This article functions as a brief introduction to Elixir and what problem it solves. For more details on the concepts explained in this article and more. I would like to refer you to the official Elixir website at http://elixir-lang.org. We would love you to join the community and say hi on our slack channel.