shahryarjb

shahryarjb

Guard `in` list doesn't work in function's parameters

Hello , I have 3 fn which create and check list, but it does not work for me .

for example :

list creator :

defp string_to_array(string) when is_binary(string) do
        String.split(string, [" ", ","])
end

and check fn :

defp merge_db_plug_category(query, group_acl) when group_acl in ["admin"] do
		IO.puts "1"
		IO.inspect group_acl
end

defp merge_db_plug_category(query, group_acl)  do
		IO.puts "2"
		IO.inspect group_acl
end

I get ["admin"] list when I send and inspect group_acl, but always merge_db_plug_category(query, group_acl) works , why doesn’t when group_acl in ["admin"] work for me ?

how can I check list in function parameters ?

Most Liked

NobbZ

NobbZ

[:foo] is not in [:foo], but :foo is.

NobbZ

NobbZ

Set functions aren’t expressable in a guard safe way you need to move the check into the body of the function or use recursion over the list and build a map of valid entries/roles where you then can again pattern match on.

LostKobrakai

LostKobrakai

Afaik you cannot do that in guards or via pattern matching.

How about

Enum.reduce(group_acl, query, fn 
  "admin", query -> 
    IO.puts "1"
    query
  _, query -> 
    IO.puts "2"
    query
end)

Where Next?

Popular in Questions Top

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
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
ovidiubadita
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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