WestKeys
Pattern match on same argument multiple times in named functions
In an anonymous function you can pattern match on same argument multiple times
color_of = fn
"fire" ->
IO.puts "red"
"water" ->
IO.puts "blue"
"grass" ->
IO.puts "green"
_ ->
IO.puts "dunno"
end
color_of.("fire")
Can this be done in a named function?
Marked As Solved
rkma
You can have multiple named functions definitions:
def color_of("fire"), do: IO.puts "red"
def color_of("water"), do: IO.puts "blue"
def color_of("grass"), do: IO.puts "green"
def color_of(_), do: IO.puts "dunno"
color_of("fire")
Also Liked
Nicd
Also, when you have multiple clauses, you can have a header where you attach the @spec and @doc annotations:
@doc "Outputs color of given material according to magic rules"
@spec color_of(String.t()) :: :ok
def color_of(material)
def color_of("fire"), do: IO.puts "red"
def color_of("water"), do: IO.puts "blue"
def color_of("grass"), do: IO.puts "green"
def color_of(_), do: IO.puts "dunno"
rkma
Indeed, it is idiomatic. Have a look at Elixir’s Enum module, for example: elixir/enum.ex at v1.11.3 · elixir-lang/elixir · GitHub
eksperimental
A similar question was asked last week, you may be interested in checking it out.
https://forum.elixirforum.net/t/suggestion-implicit-case-block-in-functions/37135
dimitarvp
Yes, this is idiomatic Elixir.
The function names aren’t duplicates; think of them as separate arms of a huge case clause (because they get compiled to exactly that).
It’s a readability convenience that hacks our brain’s ability to perceive separate written blocks as separate semantic units.








