beno

beno

Pattern match with multiple match options

I will often find my self writing things similar to:

case some_value do
  nil -> something()
  "" -> something()
  _ -> something_else()
end

So in other words I have multiple match clauses that resolve to the same outcome. I makes me want to write something like:

case some_value do
  nil | "" -> something()
  _ -> something_else()
end

Of course in this case you’d probably turn the clauses around, but that is usually not desirable or in sync with my thinking about the problem. I can think of other solutions as well, but what is the most Elixiry way of tackling this?

Most Liked

OvermindDL1

OvermindDL1

To be specific, the right argument of the in/not in operator must be statically known at compile-time when used in guard position, so you cannot do something like:

l = [1, 2, 3]
case 2 do
  b when b in l -> b # Fails compile with an ArgumentError
  _ -> nil
end

This is because in guard position the in operator works differently than in non-guard position (I really dislike inconsistencies like this), specifically it gets expanded, so the above example when written like:

case 2 do
  b when b in [1,2,3] -> b
  _ -> nil
end

Actually gets compiled to (you can check with a core erlang dump):

case 2 do
  b when b == 1 -> b
  b when b == 2 -> b
  b when b == 3 -> b
  _ -> nil

EDIT: It also works with ranges:

case 2 do
  b when b in 1..3 -> b
  _ -> nil
end

Gets turned into:

case 2 do
  b when b >= 1 and b<=3 -> b
  _ -> nil
end
12
Post #7
net

net

case some_value do
  value when value in [nil, ""] -> something()
  _ -> something_else()
end
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think your overall point is good but this is in fact false, both in matches and in the general case:

iex(1)> match?(x when x in [1.0], 1)
false
iex(2)> 1 in [1.0]
false

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement