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], fn x -> 
    cond do
   #check if tuple elem is 0
        elem(x,1) = 0->

The error is here… == not =

1 Like

Wow, and here I thought I was pattern matching. Didn’t know there was == in Elixir.

You should use == when You want to compare, and = when You want to assign, or pattern match :slight_smile:

Pattern-matching would be done with case rather than cond:

case x do
  {_, 1} -> "hello"
  _ -> ...

cond is more like a chain of if/else if

It is also possible to write an anonymous function with multiple heads., fn 
  {_, 0} -> "hello"
  _ -> ...

No need for a case :slight_smile:

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