chrisdel101

chrisdel101

How to use conditional inside Enum iterator - error:cannot invoke remote function Kernel.elem/2 inside a match

When first or last item in list is found, do something.

Even simply trying to check for this fails with error. How can I do it? Below is checking if item tuple index 1 is 0 (first item in list).

l = [owner: 0, developer: 1, admin: 2, editor: 3, contributor: 4, viewer: 5]

Enum.map(l, fn x -> 
    cond do
   #check if tuple elem is 0
        elem(x,1) = 0->
          "hello"
        end
      end)
    end

Marked As Solved

kokolegorille

kokolegorille

The error is here… == not =

Also Liked

kokolegorille

kokolegorille

It is also possible to write an anonymous function with multiple heads.

Enum.map(l, fn 
  {_, 0} -> "hello"
  _ -> ...
end)

No need for a case :slight_smile:

UPDATE: it should be {_, 1} → “hello”

Last Post!

kokolegorille

kokolegorille

It is also possible to write an anonymous function with multiple heads.

Enum.map(l, fn 
  {_, 0} -> "hello"
  _ -> ...
end)

No need for a case :slight_smile:

UPDATE: it should be {_, 1} → “hello”

Where Next?

Popular in Questions Top

rms.mrcs
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
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement