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?

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")
4 Likes

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"
4 Likes

Response on point, but I should have mentioned I had considered this option which is more verbose due to function name duplication. I am guessing this is the idiomatic approach however?

Even more verbose but definitely makes it self-documenting, even without the @doc and @spec.

Can we confirm this is idiomatic elixir ?

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

2 Likes

A similar question was asked last week, you may be interested in checking it out.

2 Likes

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.

2 Likes