Dipping my toes into Elixir

food wood nature night

So I recently (finally) decided to take the plunge and take a serious look at Ruby and Rails, so started pinging a colleague of mine who spends most of his day pounding around on a custom Rails app.  After a couple weeks or so of setting up environments, digging through books, and plowing through the Ruby Koans, he asked me if I’d heard of a new language called Elixir and the Phoenix framework that has been creating quite a stir in Ruby circles…

Think of Elixir as the love child of Erlang and Ruby, but prettier than Erlang with a simpler syntax than Ruby.  If that doesn’t pique your interest a bit, consider that although it’s a relatively new language, it executes on a mature VM that is battle-tested and natively supports concurrency way beyond C10K, distributed applications, and hot-swapping code.  NOW are you interested?

While Elixir itself is new, its roots are buried deeply in Erlang.  The Erlang language first saw light in 1986, built as a high-concurrency, high-availability solution for the telecommunications industry, backed by Ericsson.  Unlike most other languages at the time, Erlang went down the path less travelled, emerging as a functional programming (or FP) language whereas most mainstream languages at the time were of the imperative flavor.  Without getting buried too deeply in the finer points of the different paradigms, suffice to say imperative languages (C, Java, Python, PHP, Ruby, etc ad nauseum) are geared towards changing a program’s state whereas functional languages leverage functions (and often heavy recursion) to generate output based on their inputs without changing application state.  I know, kinda confusing and sounds like splitting hairs, but it does require a shift in how you approach challenges.  It is also the secret sawse behind the concurrent and distributed abilities of the language.

So, why hasn’t Erlang taken over the world?  For one thing, it was originally scoped for telecomm uses, and wasn’t viewed as a general-enough solution for everyday server work.  There were also questions about the viability of a language created in-house at a corporation instead of being an ISO-standardized language (remember what it was like in 1986?).  Functional programming was also eschewed for imperative languages in mainstream education as it was perceived to be too difficult to learn, so not many students have seen it (one LISP class was about it for me in three years of advanced programming coursework).  Finally, Erlang was a bit – well – to be honest, cryptic and often hard to read and write, which made the learning curve a bit steeper than languages that flowed more like Western languages.

So that was then.  Now Erlang is being utilized in some incredibly performant applications, and the BEAM virtual machine is almost into its third decade of existence with all the trimmings, stability, and well-understood behavior one would expect of a mature language offering.  In an attempt to leverage all this Erlang goodness and get a large community of backend developers involved from the beginning, José Valim crafted Elixir in 2012.

Admittedly, I haven’t built anything Earth-shattering in Elixir yet. I’m still plowing through tutorials, sample code, and books like Programming Elixir and Programming Phoenix while mucking around with simple distributed and concurrent apps.  See what I did there?  When was the last time you saw the words simple, distributed and concurrent all in the same sentence, much less the same paragraph?  I’m still trying to break old imperative habits (been slinging code for almost 30 years now) but enjoying the opportunity.  It just feels right, and the language and wonderful toolchain for it just work.  No jumping through hoops to get all versions correct, no trying to bailing-wire-and-bubblegum packages together to get concurrency and distributed functionality to work.

It feels like a movement and technology that are coming into their own, and the excitement around it is almost palpable.  The 2nd Annual ElixirConf US was held this October, and meetups, camps and conferences are popping up all over the world – literally.  The latest hot thing is a high-availability web framework called Phoenix developed by Chris McCord that has been benchmarked to be up to 15x faster than a fully-optimized comparable Rails implementation with full REST and channels support baked in.

That all being said, I think I’ll shelve my Ruby and Rails books for now, and focus on Elixir and Phoenix as I’ve got some ideas as to what I could do with them already… Shhhh…