Knowing Elixir

Gabriel Augusto
bawilabs
Published in
4 min readJun 28, 2017

Well, that’s a beginning even for me… So let’s hold hands and get started!

First things first, right?

Elixir is a functional language that is dynamic and compiled.

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

So Elixir is made for doing scalable and minimal applications. Let’s talk about that:

Scalability

Elixir code run inside processes that are lightweight threads. They run isolated and exchange information via messages. Because of their lightweight nature, it’s not uncommon to have hundreds of thousands of processes running on the same machine.

Isolation allows processes to be garbage collected independently, reducing system-wide pauses and using all machine resources as efficiently as possible.

Processes can also communicate with other processes from different machines on the same network.

Fault-tolerance

The unavoidable truth about software running in production is that things will go wrong. Even more when we take network, file systems, and other third-party resources into account.

In order to cope with failures, Elixir provides supervisors which describe how to restart parts of your system when things go awry. Thanks to these supervisors, everything goes back to a known initial state that is guaranteed to work.

Now that we know a little of what Elixir can do, let’s try something…

Of course we need to install it first, so go ahead and access Elixir download page https://elixir-lang.org/install.html and install it.

Let’s do you first function!

All functions must be a part of a module, so let’s make one.

Use defmodule moduleName to declare a module, and use def functionName(params) to declare a function.

Take a look at this example:

Now we need to call your function, to do that, do as it follows:

Notice that the file must have the type .exs. To execute the file, just call elixir account.exs

There you have it! Our first function on Elixir!

How about another step?

I present you the pipe operator |>!

When you are working using functional paradigm, there are no objects or classes, only functions. And not a normal function, a pure function.

For a function be considered pure, it should follow 2 rules:

  1. The function always evaluates the same result value given the same argument value(s).
  2. Evaluation of the result does not cause any semantically observable side effect or output.

The pipe operator comes to solve a problem of pure functions: nested calls.

Here an example:

g(f(x), h)

As you can see, the function g has the function f as a parameter together with some other value. That’s not so bad, but think what it can become…

With the |> operator the function change to this:

f(x) |> g(h)

The pipe operator takes the output from the function in the left and passes it as the first argument in the next function call. Almost like the | operator from Unix.

Let’s try it on a simple code:

This function takes the full name as a single argument and returns a version of the name formatted. We use the pipe operator 2 times:

  1. when passing the full name to the String.split function
  2. when passing the result from the function String.split to the function format

And that’s enough for a quick start, right?

Oh, can your problem be resolved with a functional language? Then choose Elixir!

--

--