Elixir Collections, Explained with Beers

Léonard Hetsch
7 min readJul 19, 2019

Elixir is a language I love to work with, both for personal projects and for my day-to-day job. That is the case for many reasons — such as the performance of the Beam VM, the OTP platform that allows to design robust, fault-tolerant software — and also because of the language itself, which enforces functional programming in order to create more predictable code.

One of the keys to get the best from functional programming — as in many other languages — is the correct use of data structures that come built-in with the language.

Last time, I was explaining some of the particularities of collections (or enumerables) in Elixir to a friend — such as the difference between using Enum functions and Stream functions on lists, or why prepending an item to a list with the | operator is usually better than appending new elements with ++.

It actually happened that we were having a beer at the time of that discussion, and we ended up having a good laugh comparing lists of Elixir variables to beers.

This is how I had the idea of writing this post in order to have a better understanding on how collections work in Elixir. Simply because, well, I like Elixir, and I do like beers as well. Why not put the two of them together?

Should we have one beer?

Let’s start by defining the representation of a beer. When we talk about a beer, this is usually what we picture in our mind:

--

--

Léonard Hetsch

Software engineer based in London / Technical coach @makersacademy / Previously @stuart @dicefm & @oncetheapp / Studied @gobelins_paris / Hungry learner.