Comprehensions in Elixir have the following syntax:
For example:
Generators such as x
are named local variables you can use in the expression
argument.
Note that variable assignments such as x
in comprehensions are local in scope, so it doesn’t affect variables with the same name in the outer scope.
We can have multiple generators x
and y
:
We can use filters alongside generators. When the condition in the filter evaluates to false, that particular iteration is skipped.
Unlike guard clauses, you can call your own module’s methods within filter.
The into
parameter takes in a collection for storing the comprehension’s results:
We can also use pattern matching in comprehensions: