Part 0 · 1 chapters · ~8 min

Basics

Installing Elixir with mise or asdf, iex, basic types (integers, floats, atoms, strings, lists, tuples, maps), immutability, the match operator, modules and named functions, multi-clause functions and guards, anonymous functions, the pipe operator, Enum and Stream, and mix new.

1

First code

code
defmodule Fees do
  # multiple clauses chosen by pattern and guard
  def fee(_amount, :ussd), do: 2_000
  def fee(amount, :app) when amount <= 500_000, do: 1_000
  def fee(amount, :app) when amount <= 5_000_000, do: 2_500
  def fee(_amount, :app), do: 5_000
end

transfers
|> Enum.filter(&(&1.status == :completed))
|> Enum.map(&(&1.amount_kobo + Fees.fee(&1.amount_kobo, &1.channel)))
|> Enum.sum()

{:ok, account} = Accounts.fetch("acc_1")     # raises MatchError if it returns {:error, _}
# iex> elixir --version → Elixir 1.19.5 (compiled with Erlang/OTP 28); running on OTP 29 here
ELIXIR IN SIX IDEAS
what is different from JavaScript or Python
immutable dataValues never change; functionsreturn new values.= is a matchx = 1 binds; {:ok, v} = resultdestructures or fails.the pipedata |> f() |> g(): read left toright.atoms and tuples:ok, :error, {:ok, value}: theidiom for results.modules and functionsNamed functions in modules;multiple clauses by pattern.processesConcurrency is spawning processes,not threads (part 4).
swipe the figure sideways, or tap expand for full screen
1/4
immutability
Data is immutable: Map.put returns a new map. There is no shared mutable state to race on.
no mutationno data races