Introduction to Erlang and Elixir

Elixir is a fast, dynamic and scalable language which is fast becoming adopted by the startup crowd and established businesses alike for production applications.

Introduction to Erlang and Elixir

Pinterest, Brightcove, Discord, and Canvas, to name a few, all run on Elixir, which in turn leverages the low-latency, fault-tolerant Erlang VM, meaning complete access to the Erlang ecosystem used by companies such as Heroku, WhatsApp, Klarna, and Basho.

Starting with this tutorial, you’ll learn the fundamental knowledge to get yourself started in Erlang and coding with Elixir.

What’s the Syntax Like?

The syntax is functional and promotes a short, fast coding style, which allows users easy abstraction to their data:

%User{name: name, age: age} = User.get("John Doe")
name #=> "John Doe"

When combined with guards, we have a powerful structure:

def serve_drinks(%User{age: age}) when age >= 21 do
  # Code that serves drinks!
end

serve_drinks User.get("John Doe")
#=> Fails if the user is under 21

Can It Scale?

Affirmative; Elixir was built with scalable, distributed systems in mind.

Elixir features threaded execution (referred to as processes) in an environment in which multiple processes can communicate with one another via messages.

These light-weight threads can be run in the hundreds of thousands concurrently. Elixir’s excellent garbage collector works for each isolated thread, ensuring performance is optimal system-wide and preventing resource lock-ups.

Fault Tolerance

Elixir has Supervisors which can restart parts of your system if things go wrong and revert your system to an initial state that is known to work.

How to Get Elixir

Install Elixir on your machine before we continue:

Mac OS X

  • Homebrew
    • Update your homebrew to latest: brew update
    • Run: brew install elixir
  • Macports
    • Run: sudo port install elixir

Unix (and Unix-like)

  • Arch Linux (Community repo)
    • Run: pacman -S elixir
  • openSUSE (and SLES 11 SP3+)
    • Add Erlang devel repo: zypper ar -f http://download.opensuse.org/repositories/devel:/languages:/erlang/openSUSE_Factory/ erlang
    • Run: zypper in elixir
  • Gentoo
    • Run: emerge --ask dev-lang/elixir
  • GNU Guix
    • Run: guix package -i elixir
  • Fedora 21 (and older)
    • Run: yum install elixir
  • FreeBSD
    • From ports: cd /usr/ports/lang/elixir && make install clean
    • From pkg: pkg install elixir
  • Ubuntu 12.04 and 14.04 / Debian 7
    • Add Erlang Solutions repo: wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
    • Run: sudo apt-get update
    • Install the Erlang/OTP platform and all of its applications: sudo apt-get install esl-erlang
    • Install Elixir: sudo apt-get install elixir

Windows

Interactive Development

Elixir has an interactive mode, which we can access via the command-line prompt as so:

$ iex
Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help)
iex> c "my_file.ex"        # Compiles a file
iex> t Enum                # Prints types defined in the module Enum
iex> h IEx.pry             # Prints the documentation for IEx pry functionality
iex> i "Hello, World"      # Prints information about the given data type

Windows users will need to run iex.bat to access the interactive console. 

When we enter this mode, we can type any Elixir code and get the return instantly, so it’s good for starting to learn the language.

Let’s do some basic expressions:

ie> 2 + 2
4
ie> round(3.58)
4
iex> "hello" <> " world"
"hello world"

When we are running a script, we do that from the shell terminal as so:

$ elixir scriptName.exs

To output from a script to the terminal, we need to use the following IO class:

IO.puts "Hello world from Elixir"

What About Modules?

Modules are available for Elixir so that developers can expand the language in many ways.

Here is an example of using Elixir’s test framework ExUnit:

defmodule MathTest do
  use ExUnit.Case, async: true

  test "can add two numbers" do
    assert 1 + 1 == 2
  end
end

You can run the tests in parallel by setting async: true. In this setting, Elixir uses as many CPU cores as possible.

Meanwhile, assert can check for assertion failures in your code. Those features are built using Elixir macros, making it possible to add entire new constructs as if they were part of the Elixir language itself, meaning total customisation for whatever productivity (unit testing in this case) you may need.

More to Come!

Elixir is a powerful and versatile language used by some of the biggest apps in the world right now. Its fast compile times, light-weight threaded processes, extensibility with DSL modules, and fault tolerance provided with Supervisor make it ideal for any serious web development team. Clearly, when Elixir is fully utilised, there are massive gains to be made.

In the next part, we will continue on the available Data Types of Elixir and how to write more code, and we’ll finally get down to compiling it!