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)

Last Post!

OvermindDL1

OvermindDL1

Well they are more easily done so if they use a map instead of a list, especially with the new OTP 21 features. :slight_smile:

Where Next?

Popular in Questions Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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

We're in Beta

About us Mission Statement