From 739cdfb6095f7683e537e6b4e79f05a3ec23630b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?=
A filter is an expression that evaluates to
-
The variables in the generator patterns shadow previously bound variables, including variables bound in a previous generator pattern.
-A list comprehension returns a list, where the lists elements are the +
Variables bound in a generator expression are not visible outside the expression:
+ ++1> [{E,L} || E <- L=[1,2,3]]. +* 1:5: variable 'L' is unbound+ +
A list comprehension returns a list, where the list elements are the
result of evaluating
A bit string comprehension returns a bit string, which is +
A bit string comprehension returns a bit string, which is
created by concatenating the results of evaluating
A map comprehension returns a map, where the map elements are
- the result of evaluating
A map comprehension returns a map, where the
+ map elements are the result of evaluating
Examples:
@@ -2059,6 +2061,10 @@ KeyPattern := ValuePattern <- MapExpression [{a,1},{a,2},{b,1},{b,2},{c,1},{c,2}] +More examples are provided in
+
When there are no generators, a comprehension returns either a
term constructed from a single element (the result of evaluating
More examples are provided in
-
What happens when the filter expression does not evaluate to + a boolean value depends on the expression:
+ +If the expression is a
If the expression is not a guard expression and
+ evaluates to a non-Boolean value
Examples (using a guard expression as filter):
+ ++1> List = [1,2,a,b,c,3,4]. +[1,2,a,b,c,3,4] +2> [E || E <- List, E rem 2]. +[] +3> [E || E <- List, E rem 2 =:= 0]. +[2,4]+ +
Examples (using a non-guard expression as filter):
+ ++1> List = [1,2,a,b,c,3,4]. +[1,2,a,b,c,3,4] +2> FaultyIsEven = fun(E) -> E rem 2 end. +#Fun<erl_eval.42.17316486> +3> [E || E <- List, FaultyIsEven(E)]. +** exception error: bad filter 1 +4> IsEven = fun(E) -> E rem 2 =:= 0 end. +#Fun<erl_eval.42.17316486> +5> [E || E <- List, IsEven(E)]. +** exception error: an error occurred when evaluating an arithmetic expression + in operator rem/2 + called as a rem 2 +6> [E || E <- List, is_integer(E), IsEven(E)]. +[2,4]