A beginner's guide to the Elixir programming language
Tags: webdev, beginners, programming, elixir
Published: 2021-12-30 21:22:55

If you're looking to learn a new programming language, I recommend checking out Elixir. Elixir is a process-oriented, functional programming language that runs on the Erlang virtual machine (BEAM). The language was influenced by Ruby. This inspiration can be seen and felt in Elixir's ecosystem and tooling options. Elixir is known to be easy to learn and widely applicable within the software development industry. 

In this beginner's tutorial, I'll walk you through the Elixir language and discuss its use cases, tools, syntax, and much more.

Let's get started!

**We’ll cover**:

* [What is Elixir?](#elixirprogramming)
* [Elixir vs Erlang](#elixirvserlang)
* [Elixir features, tools, and uses](#elixirusesandtools)
* [Intro to Elixir functional programming](#elixirfunctionalprogramming)
* [Let's look at some Elixir code!](#elixircode)
* [Next steps](#nextsteps)
<br>

<a name="elixirprogramming"></a>

## What is Elixir?

Elixir is a general-purpose, functional, concurrent programming language created by José Valim. Valim worked on the Ruby on Rails team, and he decided to create Elixir after experiencing issues when trying to improve the performance of Ruby on Rails. His goal was to create a language that could **run on top of Erlang’s VM**, BEAM, and that could be compatible with the Erlang ecosystem.

The Elixir syntax **shares many similarities with the Ruby syntax** and is widely used to build fault-tolerant, scalable, and maintainable applications. The language provides scalability, concurrency, fault tolerance, and low latency. 
 
<a name="elixirvserlang"></a>

### Elixir vs Erlang

Elixir is built on top of Erlang and runs on the Erlang VM. The two languages do share certain similarities, but they also have some different applications and use cases. 

Both languages support **concurrency and fault tolerance**. You could safely choose *either* language to build a large distributed system with high availability, but Elixir has a reputation for running faster than Erlang. Elixir gets the Open Telecom Platform (OTP) from Erlang, which is Erlang’s standard library used for concurrent programming.

If you want the freedom to work with different databases and frameworks, Erlang may be the better choice. Elixir is limited in database and framework support. However, if you want the **flexibility to work with different cloud platforms**, Elixir is the better option. Elixir supports many cloud platforms, while Erlang only supports a few.

There are also some **notable syntax differences** between Elixir and Erlang:

* Erlang has certain operators that Elixir does not. In Erlang, logical `and` and `or` operators are available, but they aren’t available in Elixir. 
* In Erlang, we use a colon `:` to invoke a function using the respective module, and in Elixir we use `.` to invoke a function.
* In Elixir, we can assign variables more than once, but in Erlang, we can’t assign variables more than once. Doing so would result in an error during execution.
* In Elixir, default values for arguments are always defined, but in Erlang, they aren’t.
Overall, both languages are reliable and maintainable. In recent years, Elixir has become the preferred language of the two due to its advanced support, simple syntax, and scalability.
<br>

<a name="elixirusesandtools"></a>

## Elixir features, tools, and uses

### Elixir features and tools

Elixir has many great features such as:
* Elixir compiles to bytecode for the Erlang VM
* Metaprogramming with macros and polymorphism via protocols
* Emphasis on higher-order functions and recursion
* Lazy and async collections with streams
* Pattern matching
* Built-in tooling for things like remote debugging, dependency management, code compilation, and more
* Elixir is dynamically typed, so all types are checked at runtime rather than at compilation
 
The language also has a solid set of web development tools such as:

* **Mix**: Mix is a build tool that allows you to create projects, run tests, manage tasks, and much more.
* **IEx**: IEx, Elixir’s interactive shell, provides you with many features like auto-complete, debugging, code reloading, and more.
* **Phoenix**: Phoenix is known to be one of the best web frameworks. It’s based on the MVC architecture just like Ruby on Rails.
 
### Elixir uses

Elixir is great for web applications of any size, web APIs (such as JSON or GraphQL), event-driven systems, distributed systems, internet of things, embedded systems, and much more. Many top companies use Elixir:
* **Discord**: Discord used Elixir to build their service and in 2017 successfully handled five million concurrent users and millions of events per second.
* **Moz**: Moz used Elixir on their backend to decrease their disk space and improve the speed of their API.
* **Pinterest**: Pinterest uses Elixir for its notification system to minimize the number of servers they use and clean up their code. Elixir helped them improve the performance of their notification system to deliver 14,000 per second.
* **Adobe**: Adobe used Elixir to build a client/cloud app for collaborative photography workflows.
<br>

<a name="elixirfunctionalprogramming"></a>

## Intro to Elixir functional programming

Elixir is a functional programming language. With functional languages like Elixir, we can make better use of our CPU multi-cores and write shorter and more explicit code. To better understand functional programming, I should first introduce the following fundamental principles: **immutability**, **functions**, and **declarative code**.

In functional programming, all values created in the program are immutable. By default, each function has a stable value, which means that lock mechanisms aren’t needed. This simplifies parallel work. Immutability is showing up more in conventional programming languages. These languages typically provide the immutable mechanism by giving an immutable data type alternative or a method to turn a value immutable. 

Next, let’s talk about functions. In functional programming, functions are the **primary tools for building programs**. It decreases the complexity of building large apps when functions have the following properties:

* Immutable values
* The result of the function is only affected by the function’s arguments
* The function doesn’t generate effects beyond the returned values

These functions are also known as **pure functions**. There are more complex and unpredictable functions, which are known as **impure functions**. In functional programming, values are always passed explicitly between functions, which makes the inputs and outputs clear. Functions can also be used in arguments and results of functions.

Finally, let’s take a quick look at declarative code. Declarative programming focuses on ***what* is necessary to solve a problem**, instead of *how* to solve a problem (which is what we see with imperative programming). In general, declarative code is typically more clear and concise than imperative code. With more clear and concise code, we run into fewer problems (like bugs!).
<br>

<a name="elixircode"></a>

### Let's look at some Elixir code!

Now, we’ll take some time to look at some very basic Elixir code from the [elixir-lang official documentation](https://elixir-lang.org/getting-started/introduction.html).

#### Strings

Elixir **uses UTF-8 to encode strings**. UTF-8 is a variable-width character encoding that uses one to four eight-bit bytes to store each code point. Strings are surrounded by double quotes, like `”this”`. Let’s take a look at a simple `Hello, World!` in Elixir:

```
IO.puts("Hello, World!")
```

#### Atoms

Atoms are constants whose values are their own names. In other languages, they are called *symbols*. They’re typically used to enumerate over distinct values:

```
iex> :cat
:cat
iex> :dog
:dog
iex> :fish
:fish
```

#### Booleans

Elixir supports the booleans `true` and `false`:

```
iex> true
true
iex> true == false
false
```

#### Arithmetic operations

Let’s take a look at a couple of arithmetic operations in Elixir:

```
iex> 2 + 2
4
iex> 10 * 2
20
```

In Elixir, the divide operator `/` always returns as a float:

```
iex> 8 / 2
4.0
```

#### Keyword lists

When we have a list of tuples and the first item of the tuple is an atom, we can call it a *keyword list*. Here’s an example:

```
iex> list = [{:c, 1}, {:d, 2}]
[c: 1, d: 2]
iex> list == [c: 1, d: 2]
true
```

#### Modules and functions

In Elixir, functions are grouped into modules. An example of a module is the `String` module. Here’s an example:

```
iex> String.length("elixir")
6
```

Other modules include compilation, scripted mode, function capturing, default arguments, and more.
<br>

<a name="nextsteps"></a>

## Next steps

Congrats on taking your first steps with Elixir! It’s a functional and dynamic language used for building scalable and maintainable web applications. Elixir has a reputation for being fun and easy to learn, and there’s an active Elixir community where you can interact with other Elixir programmers. There are many companies looking for Elixir developers, meaning there are plenty of job opportunities available if you want to pick up this exciting language. There’s still so much to learn about Elixir programming, such as:

* Recursion
* Higher-order functions
* [Metaprogramming](https://www.educative.io/blog/introduction-metaprogramming-elixir)

To get started learning Elixir, check out Educative’s course [**Learn Functional Programming with Elixir**](https://www.educative.io/courses/functional-programming-elixir). In this curated course, you’ll learn the fundamentals of Elixir, advanced concepts, and how to design an app using Elixir and functional programming principles. 
*Happy learning!*
<br>
 
### Learn about other popular programming languages

* [A complete guide to web development in Python](https://www.educative.io/blog/web-development-in-python)
* [Learn Java from scratch with these easy steps](https://www.educative.io/blog/learn-java-from-scratch)
* [Level up your JavaScript skills with 10 coding challenges](https://www.educative.io/blog/level-up-javascript-coding-challenges)