Last Updated: November 21, 2025
Elixir
Functional programming on Erlang VM
Basic Syntax
# Variables and data types
name = "Alice"
age = 30
pi = 3.14159
is_active = true
# Atoms (like symbols)
status = :ok
# Lists
fruits = ["apple", "banana", "cherry"]
# Tuples
person = {"Alice", 30, "Engineer"}
# Maps
user = %{name: "Alice", age: 30, email: "alice@example.com"}
# Pattern matching
{:ok, result} = {:ok, 42}
# Functions
defmodule Math do
def add(a, b), do: a + b
def factorial(0), do: 1
def factorial(n), do: n * factorial(n - 1)
end
Pipe Operator
# Chain functions with |>
"Hello World"
|> String.downcase()
|> String.split()
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
# Result: "Hello World"
# Without pipe
result = Enum.join(
Enum.map(
String.split(
String.downcase("Hello World")
),
&String.capitalize/1
),
" "
)
Enum Module
| Item | Description |
|---|---|
Enum.map(list, fn)
|
Transform elements |
Enum.filter(list, fn)
|
Filter elements |
Enum.reduce(list, acc, fn)
|
Reduce to single value |
Enum.each(list, fn)
|
Iterate with side effects |
Enum.find(list, fn)
|
Find first match |
Enum.count(list)
|
Count elements |
Enum.sort(list)
|
Sort elements |
Processes & Concurrency
# Spawn process
pid = spawn(fn -> IO.puts("Hello from process") end)
# Send and receive messages
send(pid, {:hello, "World"})
receive do
{:hello, msg} -> IO.puts("Received: #{msg}")
after
1000 -> IO.puts("Timeout")
end
# GenServer (OTP)
defmodule Counter do
use GenServer
def start_link(initial) do
GenServer.start_link(__MODULE__, initial, name: __MODULE__)
end
def get(), do: GenServer.call(__MODULE__, :get)
def increment(), do: GenServer.cast(__MODULE__, :increment)
end
💡 Pro Tips
Quick Reference
Immutability and pattern matching are core to Elixir