Navigation
Synopsis Filter values in a List/Comprehension, Set/Comprehension or Map/Comprehension.
Syntax Exp
Types
Exp
bool
Description A filter is a boolean-valued expression. If the evaluation of the filter gives true this indicates that the current combination of generated values up to this filter is still desired and execution continues with subsequent generators. If the evaluation gives false this indicates that the current combination of values is undesired, and that another combination should be tried by going back to the previous generator.
Examples Adding a filter to a comprehension, may restrict the values that are included in the result of the comprehension:
rascal>[ X * X | int X <- [1, 2, 3, 4, 5, 6] ];
list[int]: [1,4,9,16,25,36]
rascal>[ X * X | int X <- [1, 2, 3, 4, 5, 6], X % 3 == 0 ];
list[int]: [9,36]
Filters can also be applied to values produced by several generators:
rascal>[<X, Y> | int X <- [0 .. 10], int Y <- [0 .. 10], X + Y == 10]
lrel[int,int]: [
  <1,9>,
  <2,8>,
  <3,7>,
  <4,6>,
  <5,5>,
  <6,4>,
  <7,3>,
  <8,2>,
  <9,1>
]
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.