LanguagesGetting Started with the Elixir Programming Language

Getting Started with the Elixir Programming Language

What happens when you want a programming language that combines things like polymorphism, metaprogramming, and high-level concurrency management, but you can’t find one?

Well, you create it! That’s what José Valim did in 2011 when he created the Elixir programming language.

Officially described as a dynamic, functional, and scalable language, Elixir was built on top of the Erlang VM, the perfect fit for distributed, low-latency, and fault-tolerant systems, whether they’re hosted for the web, embedded, or distributed software.

In this article, we’re going to dive into some of the features that make this language so beloved.

Setting up Elixir

The installation process is very easy, regardless of your OS. If you’re on Mac, for example, simply run the following command:

brew install elixir

For other OSes, please refer to the official installation page.

Main Elixir features

Multi-threaded

Elixir was born out of a lightweight-threaded goal. It runs within threads known as processes that do not know each other. Its threads communicate with each other via messages.

Take the following example:

current_process = self()

# Spawn an Elixir process (not an operating system one!)
spawn_link(fn ->
  send(current_process, {:msg, "hello world"})
end)

# Block until the message is received
receive do
  {:msg, contents} -> IO.puts(contents)
end

In the example above, we’re spawning a new process within Elixir that’s blocked until the message attribute “:msg” is received.

Since Elixir applications can handle many threads at a time concurrently, that’s also easier to GC them to achieve greater performance levels.

If the app is distributed through the cloud, you can also program your Elixir code to make threads in different machines communicate with each other.

Fault tolerance

When things go bad, especially if your application runs on a distributed environment, Elixir once more comes to the rescue by providing built-in fault-tolerant mechanisms.

They’re called supervisors and work by providing instructions on how to restart or recover from system failures. In other words, you program the way Elixir is going to react when something goes wrong, like some sort of initial state as a reference to restart with:

children = [
  TCP.Pool, {TCP.Acceptor, port: 8000}
]

Supervisor.start_link(children, strategy: :one_for_one)

Functional programming

You probably work with functional programming already. Most of the modern languages offer some ground of support for this programming style.

The consequence is a more maintainable, shorter, and cleaner code. Take the following code snippet:

def pay(%Payment{amount: amount}) when amount > 0 do
  # Code to process a payment
end

pay(Payment.get("ASJD37SH"))
#=> Fails if the payment amount is less than or equal to 0

Easy, isn’t it? The pro here is the readability and productivity when coding conditional pattern matching. When you decide to test this code, assertions will get way easier as well.

Extensions and DSLs

As with many other languages, it’s important to provide flexible mechanisms to allow the community to extend the default capabilities of the language. Elixir easily allows this, so you can find a huge variety of plugins and frameworks that work smoothly with Elixir, such as Phoenix Framework, for example. Apart from that, if you want to make use of tools that simplify things like creating projects, managing tasks, running tests, etc. there’s the Mix tool. The following commands will take care of creating and running the tests of an Elixir application:

$ mix new developer_elixir_app
$ cd developer_elixir_app
$ mix test

Interactive mode

After installing Elixir on your machine, you’ll have an executable tool available called iex. It stands for Interactive Elixir, a tool to provide an interactive way to test the language without creating a full project upfront.

When you enter the command iex to your terminal, the following interactive shell will open so you can interact with by typing some basic Elixir instructions:

Testing Elixir commands within iex

The interactive shell also provides auto-complete features, in case you’re not quite sure about the function desired.

Auto-completion within iex

Note also that we’re making use of Erlang’s math module, which provides a handful of options in terms of math calculations for Elixir as well. To calculate the power of two numbers, for example, you’ll do the following:

$ :math.pow(2, 3)

In which you’ll get the result:

$ 8.0

Alternatively, you can also save the code in an Elixir source code file, whose extension is .exs. To run an Elixir file, simply execute the following command:

$ elixir sample.exs

Next steps

Elixir is a modern, flexible, and robust language, paving its path in the community since 2012.

There’s much more to know about it. So, make sure to refer to the official docs for more on where to go from here. Good studies!

 
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories