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 ]#=> ** (MatchError) no match ... |
Value types
Integers |
1234 / 0xcafe / 0o765 / 0b1010 / 1_000_000 |
Floating-point numbers |
1.0 / 0.2456 / 0.314159e1 / 314159.0e-5 |
Atoms |
:fred: / is_binary? / :var@2 / :<> / :=== / :"func/3" / :"long john silver" |
Ranges |
start..end / 1..9 |
Regular expressions |
r{regexp} / r{regexp}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] # => ** (MatchError) no match ...
iex> a = 1 # => 1
iex> [1, a, 3] = [1, 2, 3] # => [1, 2, 3]
iex> a # => 2 |
Tuples
{ 1, 2 } / {ok, 42, "next"} / {:error, :none} |
iex> {status, count, action} = {:ok, 42, "next"} #=> {:ok, 42, "next"}
iex> status #=> :ok
iex> count #=> 42
|