Elixir - very simple and direct getting started guide

Elixir is a dynamic, functional language that runs on top of the Erlang VM. It has very friendly syntax and it is meta-programming aware. I've been playing around with Elixir since version 0.5 and for our happiness, this week the version 1.0 of the language was finally released. From the things that I most like about the language I've got to say that the easy tools to build applications in a convenience way is the feature that I like the most. So let's get start and see the basics of running some code and create a simple application.

In the official web site there's a page that show all the currently possibilities to install elixir in your machine. There are packages for Mac (port, brew), gentoo, arch, fedora and more. I prefer to install with the precompiled packages but for that one must isntall erlang first. Here we can see an easy way to setup our environment to run Erlang and Elixir on our machine. The current version of Elixir requires Erlang 17 or superior. So be sure to install the right version of Erlang first.

Once we setup our environment properly We have four new executables in our environment elixir that can run our elixir code. elixirc to compile the code to a .beam extension to run at the Erlang VM. mix to create and manage our app with the properly steps and dependencies. And lastly iex, and iterative tool to run and evaluate elixir code on the fly. To get start jus type > iex in your terminal and you can start running some code.

iex > name = "John"  
"John"
iex > "Hello #{name}"  
"Hello John"

With this code we could start our Hello World. iex evaluate every line of code and output the result for each of then. In this example we can see a string interpolation, a nice feature of elixir. By using #{} we can represent represent any value inside a string without concatenating it explicitly. You can try doing some simple operations in the terminal. such as sum two integer with + or concatenate two strings with <>.

One thing about Elixir and the Erlang VM. Is that there's no mutability. In Erlang if you assign a value to a variable you can't reassign it later. as if you can see bellow, it shows an error if you try to assign the value again. That happens because the = operator is not an assignment operator but a matching operator.

X = 1.  
1  
X = 2.  
** exception error: no match of right hand side value 2

At the first time we do X = 1 erlang see if X is matched to any value. If it is not matched it will bind 1 to X. Once X is bind to 1 if you try to do the match again it will see that 1 is not 2 and an error will occur. Elixir is a little bit different from that. Duo the fact that elixir is build on top of the Erlang VM immutability will maintain, but with Elixir it is possible to reassign a value to another variable more than once.

iex> x = 1  
1  
iex> x = 2  
2  

One example that we illustrates great the immutability aspect of elixir is how it handles Lists. If we create a list [1,2,3] and than we want to perform an operation at the List, like update a value from it. What Elixir will do is create another list with the new values and return it just like the docs explain. That way we are assigning a new array with the updated values to the variable a.

iex> a = [1,2,3]  
[1, 2, 3]
iex> a = List.replace_at(a, 1, 10)  
[1, 10, 3]

With the basics up and ready let's do some code that can be compiled and executed. Basically, Elixir organize everything in modules. The modules contain names functions that can have public or private assessors. Let's write a hello world file names hello.ex

# hello.ex
defmodule HelloWorld do

  def say_hello(name) do
    IO.puts("Hello #{name}")
  end

end

HelloWorld.say_hello("John")  

Now that we have our file let's run the code. To run this just type on command line elixir hello.ex and you will see the Hello World message popup. But wait, Didn't I said that elixir compiles code to run on Erlang VM. For that we have the command elixirc. If you run elixirc hello.ex it will produce the compiled file Elixir.HelloWorld.beam, which it is just the binary that will load your code into the Erlang VM. Once generated if you enter into iex from that folder you can type HelloWorld.say_hello("Paul") and your code will run. This is because it is already compiled into a .beam file.

This is the quickstart of the language itself. With this basic structure to start to understand how an Elixir app is build. The next step to go from here is to understand how mix can make our life easy by integrating some tests to our code and by help with dependency management.