In BlogPost, Elixir

Last updated 10/13/17

How To Install Elixir

The Power of Proper Tooling

As I began to acquire more expertise and experience in programming, I realized that in some ways, 50% of the battle in getting started with learning a new technology stack is getting setup with the right tooling.

I still remember the first time I setup a shortcut for setting up git status. I added a shortcut with git st. One of my coworkers showed me a shortcut gs – even shorter! Holy cow, what a difference having not to type those last few characters makes!

And that’s the power of proper tooling

Proper tooling makes you more productive and makes programming more fun. I find having a proper environment manager to help you manage different releases of a language, makes it easy to test out different versions of the language. This especially holds true for an up and coming language like Elixir. So let’s dive into the tools!

Overview of The Tools

Let’s go over some tools you can use to manage your Elixir (and Erlang) installation. Since Elixir runs on top of Erlang, you’ll want a way to easily manage both Elixir and Erlang installations. Hopefully by the end of this, you’ll have everything setup and be ready to dive into the wonderful world of Elixir.

Kiex

If you’ve used a Ruby version manager like RVM or chruby, then you can think of kiex as being equivalent to that. Kiex is a handy little tool to let you easily install and/or switch between Elixir versions. And as Elixir is a fairly new language, it’s constantly being updated.

Kerl

Kerl is the tool that lets you easily switch out and build different Erlang versions. It’s a pretty handy tool to have since newer versions of Elixir keep using newer versions of Erlang. For example, due to some recent changes in the way Elixir handles debugging messages, Elixir 1.5 lets you take advantage of this functionality but only if you have Erlang 20 installed (at the time of this writing).

asdf

I would be somewhat remiss if I didn’t mention asdf, as it is a pretty handy tool that lets you install more than just Elixir. Asdf is “an extendable version manager” and it supports Erlang, Elixir, Ruby, and more languages. In my experience you end up with a .tool-versions file in your code repositories where you’re using the tool.

But that’s a minor thing in my opinion. Overall, if I hadn’t done kiex and kerl, I might have gone with asdf. The point is, “you do you”. Pick what is most comfortable to you.

Installing Elixir with Kiex and Kerl

I’m going to give you a step-by-step overview of how to install Elixir with kiex and kerl.

If you’re used to using Ruby in your day job, you’ve probably come to appreciate the Ruby ecosystem of tools to get setup and running – bundler and rvm (or chruby or rbenv depending on your preferences).

But I’m used to RVM and other environment managers – where is the RVM for Elixir?

It turns out there’s a few options for Elixir. As I stated before, I ended up choosing Kerl and Kiex. Here is how I installed it on my Ubuntu desktop and Mac OSX laptop.

Step 1 – Installing Kiex

Kiex is like RVM in that it allows you to switch between different Elixir versions and build them. Below is a set of instructions you enter at the command line.

Step 2 – Installing Kerl, Elixir and Erlang

Because Elixir runs on top of Erlang, you need a way to build and install Erlang/OTP instances.

Below is a set of instructions you enter at the command line.

You can activate this installation running the following command:

In .bashrc (or .zshrc if you use z shell), add the following:

Later on, you can leave the installation by typing: kerl_deactivate

You can delete a build with kerl delete build 20.0.

to .bashrc (or .zshrc or any other .rc file)

Troubleshooting

If you get a debug message such as:

Add the following to your shell’s config file (.bashrc/.zshrc/.cshrc):

Other useful commands

This is referred to as “binding” the value 2 to v. If you then did v = 3, this simply points v to a memory location that contains the value 3. The memory location that contains the value 2 still exists. That’s why you refer to this operation as “rebinding the variable v to the value 3” instead of “assigning 3 to v” the way you do in object oriented languages.

From a programming standpoint, now both 2 and 3 are in memory until they are garbage collected.

How This Immutable Thing Relates to Elixir Processes and “Let It Crash”

If you hang around the Elixir community long enough, you’ll hear the phrase “let it crash”, meaning to let a process (containing data) just crash if an unexpected error happens. A process never has access to another process’s data (and hence state), and so you don’t have to worry that losing one process will affect behavior or data in another process. It’s a recipe for a more stable system.

In object-oriented languages (like Java), where data can be mutated across threads, one thread can be updating while another can be reading from a data store, and so you need to worry about “protecting” state during concurrent operations.

The beauty and implication of the “rebinding” of the variable v, is that you have a guarantee that your data in a memory location won’t change. Said another way, the “data is immutable”.

Object-Oriented vs Functional

As I said before, one thing that you should be aware of right off the bat is that you’re going to have to let go of your old notions of programming via object orientation. You’ll see what I mean by that as we look at more of the Elixir language. For now, let’s look at the first syntactical difference – the lack of classes.

Modules vs Classes

If you’re coming from the Ruby world or some other object-oriented paradigm like Java, you may be used to dealing with the concept of classes. In Elixir, there’s no such thing as a class. Instead Elixir gives you a module, which is a way to group functions together.

The other interesting implication of not having classes is that there’s no such thing as “an instance of a class”, or in this case “an instance of a module”. You always call functions with the following syntax of ModuleName.function_name(arg1, arg2, …).

Below is an example of a Calculator module with an add function.

In a working Elixir program, you might have the Calculator perform some work with the following code:

Functions: Input X, Receive Y

Now that you’ve gotten a taste of Elixir modules and functions, I want to introduce one more analogy to help you wrap your head around the concept of functional programming. If you’ve ever taken an algebra class, you might be used to hearing about functions such as “f of x” (denoted f(x) for short).

You might have seen things like y = f(x) = x + 2 which means f(x) (or y) is the result of adding 2 to every input x. And that’s simply how Elixir operates – you take some data, transform it via a function, and voila, you have a new result, or a new piece of data to operate on.

If you haven’t taken algebra before, then you can think of it as a magician putting something into a hat (say a feather), and then pulling something else out of it, like a rabbit. One thing went in, and another came out.

Goodbye Inheritance

If you’re used to Ruby, you might be used to seeing inherited classes like the Dog class.

There’s no such thing as inheritance in Elixir. You just have modules that contain functions to operate on data.

Immutability

The other sticking point about Elixir that makes it different from an object-oriented language like Ruby is that data is immutable. In fact, hang around in the Elixir community long enough, and you’ll hear the word “immutability” thrown around a lot.

I already gave an explanation above, so I won’t rehash it. But to give it to you in the simplest terms, “data is immutable” means we never change data in memory, we only make copies of the data and transform it as necessary. The practical implication of this is that we don’t hold state in variables.

How do you do state?

This of course begs the question, then “how do you do state”? We’ll look at that in upcoming sections. For now, it’s sufficient to say that Elixir lets you hold state via a process mechanism. And it gives you tools like GenServer to help you do it.

Summary

It might take a little while for you to “empty your cup”, but hopefully the upcoming quizzes and projects will help you wrap your head around Elixir and functional programming concepts.

The Enum Module: The Answer To Ruby’s Enumerable

Everyone Loves the Count

When I was a little boy, I used to watch Sesame Street. The show had this character called Count von Count, or simply, The Count.

His job on the show was to teach kids about simple mathematical concepts like counting. Watching him made counting and other mathematical concepts entertaining and fun to apply.

But then I grew up and discovered the Enumerable module…

As a self-taught web programmer, my first language was Ruby, via learning Ruby on Rails. One thing that was new to me was the methods and the Enumerable module. As I got used to it, I found the Enumerable module quite beautiful as it enabled me to easily and elegantly operate on array collections. JavaScript has similar methods and I’d be willing to be other object-oriented languages do as well.

In fact, if type “enumerable” into Google, I get the definition “able to be counted by one-to-one correspondence with the set of all positive integers.”

In fact, Ruby’s Enumerable module does feel like it’s designed to operate on a list of integers (or other “objects”). It became such a part of my toolset what I was hoping Elixir had one…

Introducing Enum…

Fortunately, Jose Valim, the creator of Elixir, was formerly a Ruby language programmer. So I’m betting he too appreciated the Enumerable module. And hence, we have the Enum module in Elixir.

Like the Enumerable module in Ruby, Elixir also provides us with the Enum module to work with collections. Typically, you’ll find yourself operating on lists and maps.

Lists look a lot like arrays in Ruby, but we’ll stick with the Elixir way of calling them lists.

Useful Enum Module Methods

Now let’s go over some useufl Enum module methods. These are methods I find myself using over and over again in real world applications.

Enum#at

Let’s suppose we have the following list of integers bound to m.

What happens if we wanted to get the second element? In Ruby, you might do something like m[1]. You’ll find if you try that in Elixir you’ll get an error. Instead, you can use the at method of the Enum module as follows.

Enum#reduce

If you’re used to Ruby’s inject operator, you’ve probably seen things like:

The reduce method of the Enum module operates much the same way. Like all things in Elixir the major syntacitcal difference is that you call it using the module name follwed by the method name as follows:

Note that just like in Ruby’s inject method, the Enum#reduce method has an accumulator to hold the results of your computations.

Enum#map

Like Ruby, Elixir also gives you a map operator.

In Ruby:

In Elixir:

Enum#filter

Ruby gives you a handy select method to “filter” elements you want from an array. Below we select all elements from the array for which the condition holds true.

The Enum#filter method operates much the same way.

Enum#reject

Ruby’s converse of the select method is the reject method.

Enum#all?

Ruby also has an all? method which is extremely similar to Elixir’s. Or perhaps I should say it’s the other way around.

In any case, the all? method returns true if all the elements meet the condition.

Hopefully by now you’re seeing a common pattern. There are parts of Elixir’s syntax that borrow beautifully from Ruby (or should I say shamelessly copy?)

Enum#any?

Ruby also has an any? method to check if any element in a collection meets a particular condition.

Enum#to_list with a range

Summary

Like Sesame Street’s, The Count, the Enum module makes learning fun. The Enum module gives you quite a few useful methods for operating on data. I encourage you to read the documentation and discover even more methods. An upcoming quiz will ask you to make use of Enum’s methods, so stay tuned!

The List Module

The Linked List Data Structure and the CSV Reporter

I did study Computer Science in university, and one thing they covered was the linked list data structure. In Ruby (and other object-oriented languages), you basically get these for free through arrays.

Instead of arrays, Elixir has a concept called “lists”. They look very much like arrays and behave much the same way. They are an Elixir version of linked lists.

Making a YouTube CSV Reporter

While building my first simple production application, I built an Elixir application that pulled down data from YouTube and turned it into a CSV (comma-separate value) report. It pulled down publicly available metrics and emailed them to a business unit that needed them for reporting purposes.

As I built this application, I found myself operating on lists of data in Elixir. In fact, I’d say all my real world application usage of Elixir involved Lists in some way. So it’s definitely worth familiarizing yourself with the List module in Elixir.

Useful List Module Methods

Like the Array module in Ruby, Elixir also provides us with a module to work with lists. Typically, you’ll find yourself operating on lists and maps.

Lists look a lot like arrays in Ruby, but we’ll stick with the Elixir way of calling them lists.

List.first

Here is the Ruby syntax for calling first on an array.

And here is the Elixir syntax for calling first on a list.

List.last

Here is the Ruby syntax for calling last on an array.

And here is the Elixir syntax for calling last on a list.

List.flatten

Ruby gives you a flatten method for arrays as follows.

And so does Elixir.

List.foldl

Off the top of my head, I don’t know of a Ruby equivalent for Elixir’s foldl method.

But the foldl method uses a function to reduce the list from the left. Below is a code snippet with an explanation of how the list is being folded.

List.insert_at

Elixir’s List#insert_at function is probably closest to Ruby’s Array#insert function.

Here is how you insert into an array at a particular index value. We insert the value 5 at index 1.

Here is the Elixir equivalent.

List.delete_at

When deleting an element out of a list in Elixir, you simply specify the index number at which you wish to delete the element.

++ and – –

You can add elements to a list in Elixir with the ++ operator and remove them with the – – operator.


[1, 2] ++ [-1, 5] # => [1, 2, -1, 5]

t = [1, 2, -1, 5] t – [1] # => [2, -1, 5]

Summary

Lists are one of the most common data structures used in day to day Elixir development in my experience. It’s worth knowing the methods in that module as you will be using them frequently.

The Map Module

Useful Map Module Methods

Besides lists, the other data structure you’ll encounter frequently in Elixir are maps. I have found myself using them in everything I do,

Map.get

So Map’s get method lets you fetch a value by key. It’s very similar to the fetch method for a hash in Ruby.


m = %{:b => 2, “c” => 3, :d => 4}
Map.get(m, :b)
#=> 2

Map.get(m, :c)
#=> nil

Map.get(m, “c”)
#=> 3

You’ll notice from the above code, you have to be very specific about which key you use to fetch a value.

Map.put

Map’s put method is for putting a value associated with a key in a map. It can be used for adding new key and value pairs or updating old ones.

Elixir’s built-in syntax for updating maps

One cool trick I’ve learned from reading this blog post is how to do updating one or more map key values using a special syntax

Map.has_key?

Similar to a Ruby hash, Elixir gives you the ability to check if a given key is in a map.

Map.keys

Using Map’s keys function, you can get a list of keys only. This is analagous to Ruby’s keys function for Hash.

Map.values

Using Map’s values function, you can get a list of values only.

Map.replace

Map’s replace is handy in that it will alter the value associated with a key, but only if that key exists.

Map.merge

Map’s merge is another handy data transformation function I often find myself using to merge 2 maps together.

Map.drop

Map’s drop is good for removing keys from the list.

Map.to_list

Interestingly enough, I recently found out you could convert maps to a list of tuples.


m = %{:b => 2, :c => 3}
t = Map.to_list(m)
#=> [b: 2, c: 3]

List.first t
#=> {:b, 2}

Map.update

I haven’t used Map’s update too much, but it’s pretty handy for updating key values in a map. In the below example, code I used an initial value of “-1”, which is what the value of “a” would have been if it had not already existed in the map. Instead, the value of a is set to “1 times 3”, which is 3.

A word on nested maps

One thing I found that was hard to deal with was when I wanted to drop keys out of nested maps. To that end, I built a hex package called nested_filter to handle this. It’s a bit complicated at this point, but someone was kind enough to review it and if you go through the commit history you can see how it evolved.

We’ll probably come back to this later when we discuss anonymous functions, but I wanted it to mention it now in case you wanted to take a look.

Katas 1 and 2

Now let’s do some quick practice to internalize the concepts.

When I was learning the Vim text editor, I used to be amazed watching skilled Vim users. It was a bit like watching skilled martial artists.

It’s amazing and yet also a little bit intimidating, especially if you’re new.

To get their advanced skills, martial artists often train through the use of exercises called katas.

So I’m calling these Elixir quizzes and exercises kata after those simple karate exercises designed to train the mind and muscle for specific applications.

Doing the Katas

The easiest way to jump in would be to start up an iex shell and start experimenting. You can also write an elixir script as well.

Kata 1: Transforming an Integer List

Generate a list of integers from 1 to 50 and transform them into a map where each integer is a key and its value is twice that of the key. So for example, you should end up with a map that looks something like:

%{1 => 2, 2 => 4} and so on…


# Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

Output

# %{1 => 2, 2 => 4, 3 => 6, 4 => 8 # … etc.}

Kata 2: Transforming a List of Maps Into Success Tuples

Transform the following list of maps under the Input section to the structure in the Output section in the below coding sample.


# Input
list = [%{1 => 2}, %{2 => 4}, %{3 => 6}, %{4 => 8}, %{5 => 10}, %{6 => 12}, %{7 => 14}, %{8 => 16}, %{9 => 18}, %{10 => 20}]

Output

[ok: 2, ok: 4, ok: 6, ok: 8, ok: 10, ok: 12, ok: 14, ok: 16, ok: 18, ok: 20]
This one might be a bit tricky. The above output is equivalent to:

It’s a list of tuples.

Hints for Kata 2:

1) I used the Enum.reduce function, though you might be able to do it another way.

Conditionals in Elixir

Conditional Control Flow Structures

What would a programmer’s life be without conditionals?

Cond

Coming from Ruby, cond reminds me of the if/elsif/end structure in Ruby.In other languages it’s equivalent to an if/else if type of control flow.

Its use case is to match on conditions. In the example below, you can see you hit the “catch all” true condition, which is like hitting the “else” portion of an if/else conditional flow in Ruby.

Case

In production ready code, I’ve seen the case conditional used quite a bit, especially when pattern matching against success and error tuples. Below is an example snippet in the context of a Phoenix controller. The hypothetical “DoSomething” module does something with the incoming params map and then the with_params method returns a success or error tuple.

From there, we let the phoenix controller decide what to render.

The underscore _ in the case statement acts as a catchall, much like the true in the cond statement.

If

The interesting thing to note about Elixir’s if statement is that unlike Ruby, there’s no concept of elsif. So you won’t run into a lot of nested if statements (hopefully) unlike in some of the Ruby startup code bases I’ve seen.

Unless

Elixir’s unless statement follows the same pattern as its if statement.

Modules, Anonymous Functions, and Importing in Elixir

Alias and Import

Alias and import are two of the most common features of Elixir you’ll most likely be using when you first start using Elixir, especially if you’re using it in a web application (at least in my experience).

Alias

I think of the alias command as a shortcut to save you from typing. In actuality, you can do more with it.

For example, suppose you have the following module(s) defined.


defmodule Project.Calculator.Adder do
def add(a,b), do: a + b
def add(a, b, c), do: a + b + c
end
defmodule Project.Machine do
alias Project.Calculator.Adder

# we can call Adder.add thanks to alias

def add_two(a, b), do: Adder.add(a, b)
end
defmodule Project.Machine2 do
alias Project.Calculator.Adder, as: C
def add_two(a, b), do: C.add(a, b)
end

We could have also aliased Project.Calculator.Adder as something else entirely with the as option as shown in the Project.Machine2 module.

Try the source code for yourself by copying and pasting it in the iex shell.

Import

I think of import as a way to easily use functions from other modules without having to type out any part of the module name (unlike alias).

Here’s how that would look with our Calculator example.

Interestingly enough, I recently learned that import has an only option to let you only import macros or functions. You can also specify specific functions that get called as well.

You can see this in the commented line in the previous code block example with Project.Machine.

Module Attributes

If you’re used to constants in Ruby, you might be wondering what is the best way to handle those in Elixir. So far, I’ve found 2 good ways, one is through using private functions (defp my_function, do: "private_constant") and module attributes.

Let’s go back to our calculator example and see how we can use them.

There’s a bit to unpack from the above sample code (which you can paste into your iex shell and try out).

A Word On Types

Like Ruby, Elixir is a loosely typed language. Unlike Erlang, Elixir will allow you to rebind a value to a variable. That’s why you can do:

Integers and Floats

Elixir has integers and floats as its primary way of representing numbers. And it has the Integer and Float modules with functions to allow you to work with those types.

Arithmetic

One nice feature Ruby gives you is the “%” or modulus operator for finding the remainder of a division operation.

Elixir gives you this through the “rem” function.


rem(9, 3)
#=> 0

rem(6, 4)
#=> 2

Boolean

Elixir gives you true and false as boolean values. Interesting enough, they are equivalent to their respective atoms.


true == :true
#=> true

is_atom(true)
#=> true

false == :false
#=> false

Atoms

Analogous to Ruby’s symbols, Elixir has atoms. An atom’s name is its value.

You’ve seen atom keys throughout this lesson in the various example map structures we’ve shown.

Strings

Strings in Elixir are UTF-8 encoded and you use double quotes to represent them.

Note: single quotes denote character lists and mean something else entirely.

Lists

Lists are analogous to arrays in Ruby. We’ve seen them throughout the course in various examples.

Tuples

One interesting type that you don’t have an analogy for as a Ruby programmer is the tuple. You define a tuple with curly brackets. Tuples can hold any kind of value and store data contiguously in memory.

What does this mean practically? It means you can use the Kernel#elem method to get a tuple element by its index is a fairly fast operation.

Recent Posts