The following article has been contributed by Imobach González Sosa, Software Engineer at the YaST team, SUSE.
Apart from being fun, I truly believe that learning new programming languages helps you to become a better developer. And that’s specially true if the languages you learn push you out of our comfort zone.
Some months ago I discovered Elixir, a functional programming language that targets on the Erlang Virtual Machine (more on that later). Elixir was developed initially by José Valim, a prominent member of the Ruby community and a Ruby on Rails developer.
In this article I’ll go through some Elixir aspects and I’ll share my view and (limited) experiences about them. Maybe you get interested in the language after reading it.
Enters Functional Programming
I’ve been writing Ruby code for quite some years and I would say that it is my main programming language. As you may know, Ruby is a (highly dynamic) Object Oriented language, so approaching to Elixir has been an interesting journey.
When it comes to Functional Programming, I like to highlight two core concepts: avoiding (limiting) side-effects as much as possible and separating data and behavior. And those concepts challenged an Object Oriented mindset guy like me.
Elixir’s syntax is somehow inspired in Ruby. Once you get used to, it is quite nice, although I must admit that it has some oddities, most of them related to anonymous functions.
But if I had to underline some aspect, I would talk about the cool pipe operator. Instead of:
you can write:
some_data |> func1 |> func2
It’s not only about making the code more readable, which is just a matter of taste, but it also emphasizes the idea of data transformations.
Pattern matching is a key concept in Elixir. It’s used instead of assignments, conditions and even when calling functions. For instance, see the (typical) factorial example:
def fact(0), do: 1 def fact(n), do: n * fact(n-1)
Anyway, the Elixir documentation explains better than me. Of course, it’s nothing new for you if you come from languages like Haskell.
Being a functional language, recursion is at the heart of Elixir. As recursion will be used even for iterating, Elixir implements tail call optimization, so it will be quite efficient and you won’t have to worry, when done right, about the stack size.
Any Elixir program can be described by its own data structures, and mechanisms to compose them are available. On the other hand, the language can be extended through the use of macros. In fact, some of the language constructions are just macros.
Ecto, a database wrapper, offers a really nice macro-based API that worth a look if you’re interested in metaprogramming with Elixir.
Polimorphism, sigils, doctests… there are a lot of things to learn about Elixir, but you have many good resources available for that. So let’s move on to a different topic.
Elixir runs on top of the Erlang Virtual Machine, which means that it has access to all the resources available in that platform (just like Scala or JRuby have access to the resources on the Java Virtual Machine).
Erlang has a mature ecosystem that was built with concurrency and fault tolerancy in mind and has proven its value in quite demanding scenarios.
A crucial component on the Erlang ecosystem is the popular Open Telecom Platform (OTP). Elixir embraces OTP adding its own abstraction on top of it. Usually, complex applications are composed by several (cheap and lightweight) Erlang Virtual Machine processes which communicate and cooperate.
But when it comes to tooling, I would highlight Mix, the build tool that comes with Elixir. Apart from managing your project’s dependencies, it includes several tasks to compile, build, run tests, etc. If you come from Ruby, you can think of it like a Rake+Bundler combo.
Finally, just because it’s cool, I like to bring your attention on the Observer. This Erlang module allows you to inspect almost every aspect of your running application: load, processes, memory content… sometimes I wished having something like this in Ruby. Just check the screenshot.
Before joining the YaST team at SUSE, I was writing Ruby on Rails applications for some years (I started with version 0.13 back in 2005). For that reason, the Phoenix Framework caught my attention when I approached Elixir.
Phoenix is a web development framework inspired by Ruby on Rails but with a functional approach. It’s composed by several projects, like Ecto (the database layer we already mentioned) or Plug (did anybody say Rack)?
Fortunately, inspiration works in both directions and Ruby on Rails got some ideas from Phoenix to build Action Cable. Lovely! That’s how Open Source works …
Phoenix creator, Chris McCord, wrote an interesting article summarizing similarities and differences between Phoenix and Rails two years ago.
To be honest, if I had to start now a web development project, I would consider Phoenix as a serious alternative. Maybe during next Hack Week …
And now: have a lot of fun!