-
-
Save akhtarja/a249082b64e66f995f2af0033c45066f to your computer and use it in GitHub Desktop.
Blog post 2 - The Magic of Pattern Matching in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[a, b, c] = [:pattern, "matching", 100] | |
a #=> :pattern | |
b #=> "matching" | |
c #=> 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{first_name: first_name} = | |
%{first_name: "Javed", last_name: "Akhtar"} | |
first_name #=> "Javed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{middle_name: middle_name} = | |
%{first_name: "Javed", last_name: "Akhtar"} | |
** (MatchError) no match of right hand side value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{first_name: first_name, last_name: "Akhtar"} = | |
%{first_name: "Javed", last_name: "Akhtar"} | |
first_name #=> "Javed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{first_name: first_name, last_name: "Smith"} = | |
%{first_name: "Javed", last_name: "Akhtar"} | |
** (MatchError) no match of right hand side value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_integer(value) do | |
case value do | |
1 -> {:ok, "one"} | |
2 -> {:ok, "two"} | |
3 -> {:ok, "three"} | |
_ -> :error | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_integer(1), do: {:ok, "one"} | |
def process_integer(2), do: {:ok, "two"} | |
def process_integer(3), do: {:ok, "three"} | |
def process_integer(_), do: :error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment