Skip to content

Instantly share code, notes, and snippets.

@akhtarja

akhtarja/1.ex Secret

Last active June 1, 2020 03:57
Show Gist options
  • Save akhtarja/a249082b64e66f995f2af0033c45066f to your computer and use it in GitHub Desktop.
Save akhtarja/a249082b64e66f995f2af0033c45066f to your computer and use it in GitHub Desktop.
Blog post 2 - The Magic of Pattern Matching in Elixir
[a, b, c] = [:pattern, "matching", 100]
a #=> :pattern
b #=> "matching"
c #=> 100
%{first_name: first_name} =
%{first_name: "Javed", last_name: "Akhtar"}
first_name #=> "Javed"
%{middle_name: middle_name} =
%{first_name: "Javed", last_name: "Akhtar"}
** (MatchError) no match of right hand side value
%{first_name: first_name, last_name: "Akhtar"} =
%{first_name: "Javed", last_name: "Akhtar"}
first_name #=> "Javed"
%{first_name: first_name, last_name: "Smith"} =
%{first_name: "Javed", last_name: "Akhtar"}
** (MatchError) no match of right hand side value
def process_integer(value) do
case value do
1 -> {:ok, "one"}
2 -> {:ok, "two"}
3 -> {:ok, "three"}
_ -> :error
end
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