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
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
3
Also Liked
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
2
cd-slash
Ahh that seems so easy (and nice) now that I see the solution! Thanks for your help ![]()
1
Popular in Questions
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
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
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
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
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
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
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New







