Last active
July 28, 2017 08:14
-
-
Save PragTob/39514ae5d48f4da8912f1392fd4cc5e7 to your computer and use it in GitHub Desktop.
Elixir Pattern matching
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
defmodule Patterns do | |
def greet(%{name: name, age: age}) do | |
IO.puts "Hi there #{name}, what's up at #{age}?" | |
end | |
def greet(%{name: "José Valim"}) do | |
IO.puts "Hi José, thanks for elixir! <3" | |
end | |
def greet(%{name: name}) do | |
IO.puts "Hi there #{name}" | |
end | |
def greet(_) do | |
IO.puts "Hi" | |
end | |
end | |
Patterns.greet %{name: "Tobi", age: 27} # Hi there Tobi, what's up at 27? | |
Patterns.greet %{name: "José Valim"} # Hi José, thanks for elixir! <3 | |
Patterns.greet %{name: "dear Reader"} # Hi there dear Reader | |
Patterns.greet ["Mop"] # Hi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment