tl;dr–I’ve used Elixir for a month and mostly like it

Elixir is a functional programming language that’s built on Erlang/OTP with a fast growing community. It boasts an impressive feature set: scalability, fault-tolerance, hot-swappable code and effortless concurrency out of the box.

Having built many applications in Java and Javascript, the functional nature and forced immutability of data in Elixir requires a slightly different approach to program structure. However, the simplicity of the language, thoroughness of documentation, and readability of code notably reduces the learning curve.

Introduction

I started the Elixir journey with Saša Jurić’s book, Elixir in Action. Along with reading, I also joined the Elixir track on Exercism (an free online platform with thousands of mentored coding challenges).

I’m most surprised by the expressiveness of the language. Contrast my recursive solution to the Roman Numeral problem in Elixir, with similar solutions in Javascript. That was before I discovered single-line function declarations, and still the Elixir code is notably more readable than a comparable solution is many object-oriented languages.

The Familiar

Oddly enough, much of the language feels familiar. Elixir adopts many common paradigms, often with added improvements.

Pipe Operator

Take the case of method chaining, which in Javascript might look like:

The same code can be written using the pipe operator in Elixir:

It can even be inlined:

Yet both examples using the pipe operator seem more clear than:

The pipe operator takes the result of one expression, and passes it (as the first parameter) to the next expression. This produces code that visually approximates how the English language is read: from left to right.

Guards

Guards are another welcome language feature. Guards evaluate boolean expressions to determine which function declaration to invoke. Although they are largely syntactic sugar, in a recursion-focused language like Elixir they help clarify branching patterns.

Take the classic example of Fibonacci in Javascript:

Which can be written using guards in Elixir:

In both examples the base cases are defined, but it is more explicit in Elixir. Another use case is method overloading:

Greet.hey can accept an atom (i.e. Greet.hey(:Joe) ) or a binary (i.e. Greet.hey("Joe") ), which makes for a more convenient API. It is also possible to handle each case independently.

Annotations

Technically module attributes, Elixir provides a mechanism for declaring information at compile time. The most prominent usage being typespec.

Elixir is dynamically-typed, but with tools like Dialyzer you can perform static code analysis. Module attributes come with similar accessibility as annotations in Java in terms of meta-programming.

It’s said that Elixir is a couple of functions with some glue, which is not a huge deviation from event-driven development. If only Javascript had some of these features, it might help escape from callback hell.

The Not So Familiar

The more challenging aspects of learning Elixir have been pattern matching, recursion, and list manipulation.

Pattern matching

Pattern matching is powerful because, along with recursion, it minimizes the use of temporary variable assignment and nested conditionals. While it may look like assignment, it is both more complex and more capable.

In some ways it is akin to destructuring in ES6, where you unpack an object or array and assign the parts of interest to locally-scoped variables.

Pattern matching lends itself extremely well to recursion, which allows you to decompose transformations into a series of steps easily. — ABC_AlwaysBeCoding

Lambdas

Another interesting feature of Elixir is the ability to write lambdas without explicitly defining arguments. Compare Javascript’s arrow functions:

With lambdas functions in Elixir:

Or with the capture operator:

While the capture operator appears counter to the focus on clarity, it does offer a welcome form of concision if limited to simple use cases.

Conclusion

While others have bid object-oriented programming goodbye, I am not ready to give up on languages like Java or Javascript just yet. Both languages have a large number of libraries and frameworks that have adapted and extended them to fit a variety of contexts from mobile development to distributed web services. But, there are also downsides to functional programming in general and to Elixir specifically, which I hope to share in another article.

Nonetheless, I am pleased with how easy it has been to learn Elixir, at least to a degree of basic proficiency. I hope to get the opportunity to work on web services with open-source tools like Phoenix, Distillery, and Absinthe soon.