Getting Functional With Elixir

Recently a colleague of mine mentioned they had been taking a look at the Elixir programming language. “It’s like Ruby, but more functional”, he said, and posted a few examples in chat. And that was it. Not surprising, given that the recipients of his missive were more expert in better-established less-functional languages. Because my language of choice is snark, I too had little to say. However, I am a curious sort, and so resolved to investigate this mysterious new functional language.

After diligently querying the intertubes I found out that Elixir has been around since 2012. It builds on top of Erlang. Sadly, Erlang is another language I don’t know much about, but I’ve known for quite a while that I didn’t know about it, so that was a bit reassuring.

Over at the Elixir homepage we see that:

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

These all sound like good things. As a software developer I build things that are scalable and maintainable, or, failing that, attend plenty of meetings where we say those words. I also do a fair amount of web development, so cheers for that being successfully used.

Before I digress even further I would like to imagine a similar quote summing up Javascript:

Javascript is a language designed in too little time which has lasted longer than anyone expected it to.
Javascript leverages every browser everywhere. It terrifies you because it belies the assumption that computers perfect us. Like yourself — or any of us — it is successful in spite of its flaws.

Pretending for the moment that I was not a Javascript hack , and that I was still capable of learning new things and appreciating beauty , I took a look at the getting started guide. Installation was easy enough, and before long I was examining the language in interacitve mode via iex, and also running script files.

There is something about functional programming. It has the potential to change things for the better, but it seems somehow removed from many of the operations it might improve. It exists, we know it’s real, but where is it?

Imperative programming and functional programming

Anyway, let’s look at some code, shall we?

The following example is lifted from the getting started guide I mentioned earlier:

defmodule Math do
def sum_list([head|tail], accumulator) do
sum_list(tail, head + accumulator)
end

def sum_list([], accumulator) do
accumulator
end
end

IO.puts Math.sum_list([1, 2, 3], 0) #=> 6

When sum_list is called the definition that matches the arguments is executed. In the case above the list + accumulator args will be run through the first definition until the incoming list is empty. At that point the accumulated value will be returned. Pretty neat!

However, I’m lazy, and a terrible typist, so I wanted sum_list to use a default value of zero for accumulator. Here’s what that looked like:

defmodule Math do
def sum_list(list, accumulator \\ 0)
  def sum_list([head|tail], accumulator) do
sum_list(tail, head + accumulator)
end
  def sum_list([], accumulator) do
accumulator
end
end
IO.puts Math.sum_list([1, 2, 3]) #=> 6
IO.puts Math.sum_list([1, 2, 3], 100) #=> 106

In this new example the first sum_list definition creates the defaults, and the second two definitions are the same as before. Now, instead of:

 IO.puts Math.sum_list([1, 2, 3], 0)

The same can be achieved with 3 less characters:

IO.puts Math.sum_list([1, 2, 3])

I found the guide a helpful overview of what the language does, and strongly recommend starting there unless you already have all the answers. In the latter case, cheers, carry on.

Next Steps

No matter how well-constructed a language might be, or not be, it is not just by those merits that it becomes useful. There is also the matter of what tools are out there already built. I mean, where would Ruby be without Rails, or Javascript without (at least until recently) JQuery?

Here’s a few links to some things that use Elixir in useful ways. If you don’t like them feel free to google for better links.