Pantrypoints Pantrypoints

Pattern Matching

August 22, 2023 1 minutes  • 128 words

Pattern Matching Steps:

  1. Evaluate the contents
  2. Match them
a = 1 # if a matches 1 then ok. Since a has nothing then 1 is assigned to it

[a, a] = ["z", "z"]

[a, a] = ["x", "z"] # error

["x", "x"] = [a, a] # error because a is "z". This exposes that = is really key = value

[^a, ^a] = ["x", "x"] # error because pin operator pins the value of a to "z" which does not match "x"

[a, a] = ["x", "x"] # works because the pattern is matches even if the "x" values are new
data = %{ name: "Dave", state: "TX", likes: "Elixir" } 

for key <- [ :name, :likes ] do
  %{ ^key => value } = data
  value
end
Follow Us! →

We're creating a new Economic System from a new Economic Science! Please support us by leaving your email or leaving a comment above.