Pattern Marching and Equals Operator
import IEx.Helpers
The = (match) operator
a = 42
42
a
42
a = 42
42
The pin operator
^a = 42
# ^ = pin operator, to match a value
42
Matching Tuples
place = {:austin, :tx}
{:austin, :tx}
place
{:austin, :tx}
# {city, state} = {:austin, :texas}
{city, state} = place
{:austin, :tx}
"#{city}, #{state}"
"austin, tx"
{city, :tn} = place
{city, state, country} = place
Matching List
three = [1, 2, 3]
four = [4, 5, 6, 7]
[4, 5, 6, 7]
[first, second] = three
defmodule MyList do
def first_and_second([first, second | list]) do
"#{first} #{second} #{inspect(list)}"
end
end
{:module, MyList, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:first_and_second, 1}}
MyList.first_and_second(three)
"1 2 [3]"
MyList.first_and_second(four)
"4 5 [6, 7]"