footer: © Peter Mueller, 2019 slidenumbers: true
@felix_starman
felix-starman
- slides at: gh/felix-starman/what_is_pattern_(...)
[.code: auto(47)]
// javascript
let some_var = ["menuItem", 37, "Crunchy Frog"]
# ruby
some_var = [:menu_item, 37, "Crunchy Frog"]
# elixir
some_var = [:menu_item, 37, "Crunchy Frog"]
[.code: auto(47)]
[.code-highlight: all] [.code-highlight: none]
// javascript: destructuring assignment
[type, id, title] = ["menuItem", 37, "Crunchy Frog"]
[.code-highlight: none] [.code-highlight: all] [.code-highlight: none]
# ruby: multiple-assignment
[type, id, title] = [:menu_item, 37, "Crunchy Frog"]
[.code-highlight: none] [.code-highlight: all]
# elixir: matching "assignment"
[type, id, title] = [:menu_item, 37, "Crunchy Frog"]
[.code: auto(47)]
[.code-highlight: 2] [.code-highlight: 3] [.code-highlight: 4] [.code-highlight: 6] [.code-highlight: 7]
# elixir
fav_fruit = %{apples: ["Honey Crisp", "Granny Smith"]}
%{apples: apples} = fav_fruit
apples # ["Honey Crisp", "Granny Smith"]
%{durian: true} = fav_fruit
# (MatchError) no match of right hand side value: %{apples: ["Honey Crisp", "Granny Smith"]}
[.code: auto(47)]
[.code-highlight: 1-2] [.code-highlight: 3] [.code-highlight: 4] [.code-highlight: 6]
# Maps
%{username: _} = user_session
%{username: _, admin: true} = user_session
%{username: "demosthenes"} = user_session
%{author: ^current_user} = article
[.code: auto(47)]
[.code-highlight: 1-2] [.code-highlight: 4-5] [.code-highlight: 7-8]
# Strings
"NAME=" <> name = commandline_args # String matching
# Lists
[un, pw] = String.split("my_user:password1", ":")
# Tuples
{:ok, result} = VeryImportantThing.do_it()
[.code: auto(47)]
[.code-highlight: all] [.code-highlight: 2,4,10] [.code-highlight: 2-4,10] [.code-highlight: 2,4-10] [.code-highlight: 4-10] [.code-highlight: 5,6] [.code-highlight: 7,8] [.code-highlight: 2,4,5,7,8] [.code-highlight: all]
def authorized?(conn) do
if conn.admin do
true
else
if conn.current_user == conn.owner_of_thing do
true
else
false
end
end
end
[.code: auto(47)]
[.code-highlight: all] [.code-highlight: 1,5,6] [.code-highlight: 2,5,6] [.code-highlight: 3,5,6] [.code-highlight: all]
def authorized?(%{admin: true}), do: true
def authorized?(%{owner: user, current_user: user}), do: true
def authorized?(_), do: false
# These are "definitions" for ONE function (`authorized?/1`)
# Patterns are evaluated in the order they are defined