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
NobbZ

NobbZ

Ok, then I mid remembered the semantics of in. The issue of bindings still applies and the fact that guards do affect the performance as well. So 2 of 3 issues remaining.

NobbZ

NobbZ

This only works kind of. 1 in [1.0] is true when I’m remembering correctly. Also sometimes you want to bind parts of the different matches. That won’t work with your guard as well. Last but not least guards can’t get compiled into a trie and therefore slow down the overall match.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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 record...
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
_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
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement