Elixir Sample

Elixir is a functional, meta-programming aware language built on top of the Erlang VM. It is a dynamic language with flexible syntax and macro support that leverages Erlang's abilities to build concurrent, distributed and fault-tolerant applications with hot code upgrades.

To install Elixir or learn more about it, check our getting started guide. We also have online documentation available and a Crash Course for Erlang developers. Or you can just keep on reading for a few code samples!

Language highlights

Everything is an expression

defmodule Hello do
  IO.puts "Defining the function world"

  def world do
    IO.puts "Hello World"

  IO.puts "Function world defined"


Running the program above will print:

Defining the function world
Function world defined
Hello World

This allows a module to be defined in terms of many expressions, programmable by the developer, being the basic foundation for meta-programming. This is similar to what Joe Armstrong (creator of Erlang) proposes in his erl2 project.

Meta-programming and DSLs

Using expressions and meta-programming, Elixir developers can easily create Domain Specific Languages:

defmodule MathTest do
  use ExUnit.Case

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

DSLs allow a developer to create abstractions for specific domains, often getting rid of boilerplate code.

Polymorphism via protocols

Protocols allow developers to provide type-specific functionality at chosen extension points. For example, the Enum module in Elixir is commonly used to iterate collections:

Enum.map([1,2,3], fn(x) -> x * 2 end) #=> [2,4,6]

Since the Enum module is built on top of protocols, it is not limited to the data types that ship with Elixir. A developer can use their own collections with Enum as long as they implement the Enumerable protocol. For example, a developer can use all the convenience of the Enum module to easily manipulate a file, line by line:

file  = File.stream!("README.md")
lines = Enum.map(file, fn(line) -> Regex.replace(~r/"/, line, "'") end)
File.write("README.md", lines)

Documentation as first-class citizen

Documentation is supported at the language level in the form of docstrings. Markdown is Elixir's defacto markup language of choice for use in docstrings:

defmodule MyModule do
  @moduledoc """
  Documentation for my module. With **formatting**.

  @doc "Hello"
  def world do

Different tools can easily access the documentation. For instance, IEx (Elixir's interactive shell) can show the documentation for any module or function with the help of the function h:

iex> h MyModule
# MyModule

Documentation for my module. With **formatting**.

There is also a documentation generator for Elixir called ExDoc that can produce a static site using docstrings extracted from the source code.

Pattern matching

Pattern matching allows developers to easily destructure data and access its contents:

{ User, name, age } = User.get("John Doe")

When mixed with guards, pattern matching allows us to easily express our intent:

def serve_drinks({ User, name, age }) when age < 21 do
  raise "No way #{name}!"

def serve_drinks({ User, name, age }) do
  # Code that serves drinks!

serve_drinks User.get("John")
#=> Raises "No way John!" if John is under 21

Tooling included

Elixir ships with a great set of tools to ease development. Mix allows you to easily create your new project, manage tasks and dependencies:

$ mix new my_app
$ cd my_app
$ mix test

Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
1 tests, 0 failures

In the example above, we have created a new project and ran its initial test suite powered by the ExUnit test framework.

Interactive (and remote) shells

Elixir also ships with an Interactive Shell, called IEx, which provides a great set of helpers for writing code, like easy access to documentation (shown above), code reloading and so on. It is also a great companion for production, as it allows for example connecting to remote nodes. Here is a quick example you can try locally. In one terminal, do:

$ iex --name hello
iex(hello@machine)1> defmodule Hello do
...(hello@machine)1>   def world, do: IO.puts "Distributed hello world"
...(hello@machine)1> end

See the name in between parenthesis starting with hello? Copy that, open up another terminal and pass it to --remsh (short for remote shell):

$ iex --name world --remsh "hello@machine"
iex(hello@machine)1> Hello.world
"Distributed hello world"

Notice we were able to invoke a function from a module defined in the other terminal! Even the node names are the same. This will work in between machines in the same network as long as they have the same value in the ~/.erlang.cookie file!

Erlang all the way down

After all, Elixir runs in the Erlang VM. An Elixir programmer can invoke any Erlang function with no runtime cost:

:crypto.md5("Using crypto from Erlang OTP")
#=> <<192,223,75,115,...>>

Since Elixir compiles to the same bytecode, it is fully OTP compliant and works seamlessly with all the battle-tested techniques for which Erlang/OTP is famous. Erlang type specifications, behaviours and module attributes are all supported. It is easy to add Elixir to your existing Erlang programs too (including rebar support)!

To install Elixir or learn more about it, check our getting started guide. We also have online documentation available and a Crash Course for Erlang developers.