Show Menu
Cheatography

Elixir educative.io Cheat Sheet (DRAFT) by

Mastering the Art of Programming in Elixir 2023

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Lists

iex> list = [ 1, 2, 3 ]
[1, 2, 3]
iex> [a, b, c ] = list
[1, 2, 3]

The pin operator

iex> a = 1 #=> 1
iex> [^a, 2, 3 ] = [ 1, 2, 3 ] #=> [1, 2, 3]

iex> a = 2 #=> 2
iex> [ ^a, 2 ] = [ 1, 2 ]#=> ** (Match­Error) no match ...

Value types

Integers
1234 / 0xcafe / 0o765 / 0b1010 / 1_000_000
Floati­ng-­point numbers
1.0 / 0.2456 / 0.314159e1 / 314159.0e-5
Atoms
:fred: / is_binary? / :var@2 / :<> / :=== / :"fu­nc/­3" / :"long john silver­"
Ranges
start..end / 1..9
Regular expres­sions
r{regexp} / r{rege­xp}opts / ~r/.../
 

Ignoring a value with _

iex> [1, _, _] = [1, 2, 3]
[1, 2, 3]
iex> [1, _, _] = [1, "­cat­", "­dog­"]
[1, "­cat­", "­dog­"]

System types

PIDs and ports
The PID of the current process is available by calling self. A new PID is created when we spawn a new process.
References
The function make_ref creates a globally unique reference. No other reference will be equal to it.

List operators

iex> [1,2,3­]++­[4,5,6]
[1, 2, 3, 4, 5, 6]
iex> [1, 2, 3, 4] -- [2, 4]
[1, 3]
iex> 1 in [1,2,3,4]
true
 

Variable binding

iex> [a, a] = [1, 1] # => [1, 1]
iex> [b, b] = [1, 2] # => ** (Match­Error) no match ...

iex> a = 1 # => 1
iex> [1, a, 3] = [1, 2, 3] # => [1, 2, 3]
iex> a # => 2

Tuples

{ 1, 2 } / {ok, 42, "­nex­t"} / {:error, :none}
iex> {status, count, action} = {:ok, 42, "­nex­t"} #=> {:ok, 42, "­nex­t"}
iex> status #=> :ok
iex> count #=> 42