Kernel.in
in, go back to Kernel module for more information.
Membership operator. Checks if the element on the left-hand side is a member of the collection on the right-hand side.
Examples
iex> x = 1
iex> x in [1, 2, 3]
trueThis operator (which is a macro) simply translates to a call to
Enum.member?/2. The example above would translate to:
Enum.member?([1, 2, 3], x)Elixir also supports left not in right, which evaluates to
not(left in right):
iex> x = 1
iex> x not in [1, 2, 3]
falseGuards
The in/2 operator (as well as not in) can be used in guard clauses as
long as the right-hand side is a range or a list. In such cases, Elixir will
expand the operator to a valid guard expression. For example:
when x in [1, 2, 3]translates to:
when x === 1 or x === 2 or x === 3When using ranges:
when x in 1..3translates to:
when is_integer(x) and x >= 1 and x <= 3Note that only integers can be considered inside a range by in.
AST considerations
left not in right is parsed by the compiler into the AST:
{:not, _, [{:in, _, [left, right]}]}This is the same AST as not(left in right).
Additionally, Macro.to_string/2 and Code.format_string!/2
will translate all occurrences of this AST to left not in right.