Based on the answers here and in Elixir Slack, there are several ways to check if an item exists in a list. For the answer from @Gazler:
Enum.member?(["foo", "bar"], "foo")
or simply
"foo" in ["foo", "bar"]
or
Enum.any?(["foo", "bar"], &(&1 == "foo")
or if you want to find and return an element instead of true or false
Enum.find(["foo", "bar"], &(&1 == "foo") # "foo"
If you want to check a tuple, you need to convert it to a list (credit @Gazler):
Tuple.to_list({"foo", "bar"}) # ["foo", "bar"]
But, as @CaptChrisD pointed out in the comments, this is an unusual need for a tuple, because it is usually interested in the exact position of an element in a tuple for pattern matching.
ewH Apr 05 '16 at 22:53 on 2016-04-05 22:53
source share