WestKeys

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

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

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

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

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

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.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement