Skip to content

Instantly share code, notes, and snippets.

@PragTob
Last active July 28, 2017 08:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PragTob/39514ae5d48f4da8912f1392fd4cc5e7 to your computer and use it in GitHub Desktop.
Save PragTob/39514ae5d48f4da8912f1392fd4cc5e7 to your computer and use it in GitHub Desktop.
Elixir Pattern matching
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