A beginner's guide to the Elixir programming language
Erin Schaffer
Posted on December 30, 2021
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?
- Elixir vs Erlang
- Elixir features, tools, and uses
- Intro to Elixir functional programming
- Let's look at some Elixir code!
- Next steps
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.
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
andor
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.
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.
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!).
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.
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.
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
To get started learning Elixir, check out Educative’s course Learn Functional Programming with 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!
Learn about other popular programming languages
Posted on December 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.