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:
Get hands-on with Elixir.
Elixir is a functional programming language that runs in the Erlang VM, a powerful environment to run distributed systems. This course uses Elixir because of its fun syntax, vibrant community, and production-ready tooling. Elixir syntax lets you focus on what’s important while learning functional programming. Starting from scratch, you will learn the fundamentals of Elixir including expressions, modules, conditional statements, and recursion. Towards the end of the course, you will focus on more advanced material like higher-order functions, how to handle impure functions, and lastly how to design an application using Elixir and functional programming principles. By the end, you will be able to write basic Elixir programs that follow the functional programming paradigm.
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 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:
and
and or
operators are available, but they aren’t available in Elixir.:
to invoke a function using the respective module, and in Elixir we use .
to invoke a function.Elixir has many great features such as:
The language also has a solid set of web development tools such as:
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:
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:
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!).
Get hands-on with Elixir.
Elixir is a functional programming language that runs in the Erlang VM, a powerful environment to run distributed systems. This course uses Elixir because of its fun syntax, vibrant community, and production-ready tooling. Elixir syntax lets you focus on what’s important while learning functional programming. Starting from scratch, you will learn the fundamentals of Elixir including expressions, modules, conditional statements, and recursion. Towards the end of the course, you will focus on more advanced material like higher-order functions, how to handle impure functions, and lastly how to design an application using Elixir and functional programming principles. By the end, you will be able to write basic Elixir programs that follow the functional programming paradigm.
Now, we’ll take some time to look at some very basic Elixir code from the elixir-lang official documentation.
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 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:catiex> :dog:dogiex> :fish:fish
Elixir supports the booleans true
and false
:
iex> truetrueiex> true == falsefalse
Let’s take a look at a couple of arithmetic operations in Elixir:
iex> 2 + 24iex> 10 * 220
In Elixir, the divide operator /
always returns as a float:
iex> 8 / 24.0
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
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.
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:
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!
Free Resources