Elixir Intro
Elixir is a dynamic, functional programming language designed for building systems that stay up: chat servers, payment pipelines, IoT gateways, real-time dashboards. It was created by José Valim in 2011 and runs on the BEAM, the virtual machine behind Erlang, which telecom companies have used for decades to run switches that are expected to almost never go down.
Elixir gives you a friendly, Ruby-inspired syntax, excellent tooling (Mix, Hex, ExUnit, IEx) and a powerful macro system, while keeping everything the Erlang ecosystem is famous for.
IO.puts("Hello from Elixir!")
IO.puts("1 + 2 = #{1 + 2}")Hello from Elixir!
1 + 2 = 3
IO.puts/1 prints a string followed by a newline. The #{...} inside a double-quoted string is interpolation: the expression is evaluated and its result is inserted into the text.
Three big ideas
| Idea | What it means in practice |
|---|---|
| Functional | Data is immutable. Functions take values and return new values; nothing is changed in place. |
| Concurrent | Programs are made of many lightweight processes (not OS threads) that share nothing and talk by sending messages. A laptop can run millions of them. |
| Fault-tolerant | Processes are cheap to restart. Supervisors watch them and restart them when they crash ("let it crash"), so a bug in one request doesn't take down the whole system. |
Immutability in one example
In Elixir you never modify a list, a map or a string. Functions such as List.insert_at/3 return a new value and leave the original untouched:
cities = ["Lisbon", "Oslo"]
more = List.insert_at(cities, 0, "Kyoto")
IO.inspect(cities)
IO.inspect(more)["Lisbon", "Oslo"]
["Kyoto", "Lisbon", "Oslo"]
IO.inspect/1 prints any value in its Elixir syntax (lists with brackets, strings with quotes), which makes it the tool you will use most while learning.
A taste of concurrency
Here a second process computes a value and sends it back to the main process as a message. You will learn every piece of this in the Processes lesson; for now, notice how little code it takes:
parent = self()
spawn(fn ->
total = Enum.sum(1..100)
send(parent, {:result, total})
end)
receive do
{:result, value} -> IO.puts("The other process says: #{value}")
endThe other process says: 5050
Where Elixir is used
- Web applications with the Phoenix framework and LiveView (server-rendered, real-time UIs).
- Messaging and real-time systems: chat, notifications, multiplayer game servers.
- Data pipelines with GenStage, Broadway and Flow.
- Embedded systems with Nerves.
- Machine learning with Nx, Axon and Livebook notebooks.
How this course works
Every elixir example on this site is a script that was run with Elixir 1.19 (elixir main.exs), and the output block under it is the real output. The site cannot run the BEAM inside your browser, so the playground shows that saved output. Install Elixir locally (next lesson) to experiment with your own changes: copy an example into a file named main.exs and run elixir main.exs.
Note: Elixir code is organised in modules and functions. A script file (
.exs) may also contain top-level code, which is what most examples in this course use to keep things short.
Test yourself
Which virtual machine does Elixir run on?
- The JVM
- The BEAM (Erlang VM)
- The CLR
- V8
What does List.insert_at(cities, 0, "Kyoto") do to cities?
- It adds Kyoto to the front of
cities - Nothing: it returns a new list and
citiesstays the same - It raises an error because lists are immutable
- It replaces the first city
What are Elixir processes?
- Operating-system threads
- Lightweight, isolated units of execution that communicate by messages
- Separate OS programs started with fork()
- Async callbacks like in JavaScript