Skip to content

Latest commit

 

History

History
172 lines (140 loc) · 3.36 KB

File metadata and controls

172 lines (140 loc) · 3.36 KB

footer: © Peter Mueller, 2019 slidenumbers: true

What is Pattern Matching

and How to Use the Debugger

fit, original


Peter Mueller


[.code: auto(47)]

Pattern Matching

First let's talk about "assignment"

// 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)]

Pattern Matching

There is even multiple-assignment in some languages

[.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)]

Pattern Matching

But "Matching" is not quite assignment

[.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)]

Pattern Matching

You can match on the shape, but assign nothing

[.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)]

Matching on other data structures

[.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)]

Matching in function definitions

Little ugly

[.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)]

Matching in function definitions

Awwwwwww yeaaaah

[.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

Debugger

right fit