%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
Pattern matching presentation
1. WELCOME
• Hi everyone, I am Michael Dimmitt’s slide robot.
• 10 Minutes, your level and what you hope to learn.
2. Why Make new slides? .. pattern match a previous presentation!
🙂😅 https://www.youtube.com/watch?v=nEUHb7RJspQ
… Robotically cloning slides 😅
3. • Pattern Matching in Elixir
• What is pattern matching?
• Why should we use it?
• The basics of using it?
• A coding example
4. • What is pattern matching?
… well it is a tool we can use in elixir for our data.
• What is a pattern?
Any value that can be held in a variable.
We can pin a pattern using ^ … to enforce our
pattern matches the value inside a variable.
Pattern matching is faster than using conditionals.
An alternative for capturing data flow, if statements.
5. • Why should we use Pattern Matching and Basic Usage?
• Functions serve as case statements! (shown in next slide)
• Basic use: setting variables or checking variables.
• x = %{name: ‘John’}
• [a, b, c] = [head | tail ] = [1,2,3] = [1 | [ 2, 3] ]
• c=3;
[a, b, ^c] = [head | tail ] = [1,2,3] = [1 | [ 2, 3] ]
• Lets talk about a case statement with nested if’s
7. • Remember the previous example?
• [a, b, c] = [head | tail ] = [1,2,3] = [1 | [ 2, 3] ]
• We can use that in a function!
• def matcher( cool_list =
[a, b, c] = [head | tail ] = [1,2,3]
) do
{
‘cool_list', cool_list,
'a:', a, 'b:', b, 'c:', c,
'head:', head, 'tail:', tail
}
end
8. • How did I get into this topic?
• macrosimplifier
https://github.com/MichaelDimmitt/macrosimplifier/blob/master/lib/
macro_simplifier.ex
• Macros can seem complicated but by ignoring parts that the
machine cares about we can understand what is occurring.
• As I began to simplify, the inputs could deconstructed into different
shaped and I began to see shapes in the data.
• Improvements thought of for this project:
separate the recursion from the functions that return values of
tuple list or atom.
9. • ## Specify Size of the String.
<<name::bytes-size(4)>> <> "@gmail.com" = "mike@gmail.com"
<<name::bytes-size(4)>> <> "@gmail.com"="michael@gmail.com"
## pattern match length of string response ensuring it is 32 to match auth0 token.
def get_requester( conn ) do
case get_claims( conn ) do
%{ "sub" => auth0_user_id } -> Repo.get_by( User.auth0_user_id: auth0_user_id )
%{ "sub" => <<auth0_client_id::bytes-size(32)>> <>"@clients" } ->
Repo.get_by( ApiClient, auth0_client_id: auth0_client_id)
end
end
• More Complex Examples:
10. • Also check out the talk on pattern matching
https://www.youtube.com/watch?v=nEUHb7RJspQ
• It contains two additional examples:
• A gen-server simulating a coin turnstile with
pattern matching.
• And an example of the pin operator for whether a
particular dog meets a humans satisfaction.
11. • NextTime!
• Let’sTalk About Performance?
• Let’s talk about curried functions?
• https://medium.com/@alexander.s.boring/creating-
curried-functions-in-elixir-89bb30de8142