cd-slash

cd-slash

Boolean expression pattern matching with tuples

I have a case clause (contrived for the purpose of this question):

level = 4
has_children_or_no_children = :no_children

case {level, has_children_or_no_children} do
  {3, :has_children} ->
    1

  {4, :no_children} ->
    2

  _ ->
    false
end

This works well. Normally though, with a single value in the case clause I can use a guard to match on greater than / less than integer values:

level = 4
has_children_or_no_children = :no_children

case level do
  n when n < 5 ->
    1

  n when n > 0 ->
    2

  _ ->
    false
end

Which again works well, but I can’t use these guard clauses inside the tuple passed to case in the first example:

level = 4
has_children_or_no_children = :no_children

# DOES NOT WORK
case {level, has_children_or_no_children} do
  {n when n < 5, :has_children} ->
    1

  {n when n > 0, :no_children} ->
    2

  _ ->
    false
end

Is there a way to perform a boolean test and a comparison with an atom simultaneously, to avoid having to nest case statements and duplicate the code to be executed?

Marked As Solved

sbuttgereit

sbuttgereit

The guard needs to be outside of the match:

case {level, has_children_or_no_children} do
  {n, :has_children} when n < 5 -> 1
  {n, :no_children} when n > 0 -> 2
  _ -> false
end

Also Liked

nikolauska

nikolauska

You need to move the when outside the tuple and then it works

level = 4
has_children_or_no_children = :no_children

case {level, has_children_or_no_children} do
  {n, :has_children} when n < 5 ->
    1

  {n, :no_children} when n > 0 ->
    2

  _ ->
    false
end
cd-slash

cd-slash

Ahh that seems so easy (and nice) now that I see the solution! Thanks for your help :slight_smile:

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
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
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement