Instead, Elixir was designed to elegantly reintroduce retro 1980’s tech like Erlang and the Open Telecom Platform (OTP). What could be cooler than an open-source toolset created by Ericsson for distributed, fault-tolerant systems like telephones switches?
In reality, Erlang/OTP is a solid foundation to build any ecosystem. Telecom services don’t have the luxury of scheduled maintenance windows. Imagine a call dropping or a text message bouncing because of server updates? Imagine an improperly routed text message in the wrong inbox?
Behind the scenes it’s powerful features like green threads and supervision trees that enable OTP services to be massively scalable and highly available. These benefits aren’t limited to telephony either. Consider WhatsApp, who scaled to over 900M users with just 50 engineers thanks in large part to OTP.
So why not just learn Erlang? So what’s with all the fuss about Elixir? Why does Elixir leave some to ask: Anyone just feel happy writing Elixir code?
What Elixir brings to the table is a complete different surface syntax, inspired by Ruby. What you might call a “non-scary” syntax, and a load of extra goodies.
– Joe Armstrong (creator of Erlang)
Modules & Objects
The same functionality could be expressed in Elixir:
Math object. You can’t create a
new Math() (many have tried), but you can make use of its static properties (like
Math.PI) and methods (like
It’s argued that the lack of objects makes Elixir less capable, but what’s lost in power is gained in simplicity. Functional programming is designed, in part, to save you from yourself. Just think, in Elixir you will never have a problem with
this, because it doesn’t exist!
Functions Ride First Class
function keyword, and anonymous functions which are assigned to a variable (most commonly written in arrow notation).
This trivial example does everything mentioned above:
- Assign a function to the variable
- Pass a function
theThingas an argument to
- Invoke a function argument,
- Return a function as the result of
P.S. the last technique where one function returns another is called currying.
All of this is possible in Elixir as well:
Although Elixir uses different syntax, both languages support named and anonymous functions that can then be passed, returned, and invoked. Next we’ll see how to build complexity by combining functions.
Method Chaining & The Pipe Operator
The way to write shorter code is by using the pipe operator. – Ryan Bigg
Method chaining is another common technique in JS. In its simplest form, it involves consecutively invoking functions on the same object.
Arrays are one common use case:
We can do the same thing in Elixir using the pipe operator:
The pipe operator
|> takes the result of the first expression and passes it as the first argument in the next, reading from left-to-right, top-to-bottom.
However, when composing functions you often need only part an input–usually a certain property from an object or element in an array. That’s where destructuring comes in!
Pattern Matching & Destructuring
On the surface, pattern matching seems like a foreign concept, but put simply pattern matching is not assignment and it allows developers to easily destructure data.
Destructure… destructure… where have we heard of that? Oh yeah, ES6 brought with it a very similar feature called destructuring assignment!
This can be achieved in Elixir using pattern matching:
Both languages support extracting properties from maps and accessing lists values by position, although admittedly the Elixir version is more verbose. (note: there are tools to like the ~M sigil for more concise destructuring).
let newData = JSON.parse(JSON.stringify(oldData));
This is neither idiomatic or efficient, but without importing a library this is by far the simplest way. If this seems frustrating, that’s because it is. That’s why many languages and frameworks are moving to immutable data.
To Mutate, or Not To Mutate
setState creates a new state object.
Here it is unclear whether
fill operates in-place or returns a new array:
What it actually does is a bit of both! It reassigns values in the input array, which it then returns. This fails subsequent expectations of using this function to transform one array into a new array.
In Elixir, all data is immutable. That means variables are passed by value. Because of this, it is not possible to reassign the value of arguments nor is it possible to modify data in-place. Recall:
Functional programming is designed, in part, to save you from yourself.
— Me, earlier in this article
This ensures that functions cannot modify their input directly. Instead, they unambiguously return new data based their input.
Clearly, functional programming is the best programming! Ok, that is a bit of a stretch. Hopefully I have convinced you that functional programming is at least useful, and that Elixir is a cool functional programming language.
If you find Elixir cool and want to learn more, here are a few resources: