Navigation
Synopsis Comprehensions for generating values.
Description Comprehensions are a notation inspired by mathematical set-builder notation and list comprehensions that help to write succinct definitions of lists and sets. They are also inspired by queries as found in a language like SQL.

Rascal generalizes comprehensions in various ways. Comprehensions exist for lists, sets and maps. A comprehension consists of an expression that determines the successive elements to be included in the result and a list of enumerators and tests (boolean expressions). The enumerators produce values and the tests filter them.

See Expressions/Comprehensions, List/Comprehension, Set/Comprehension, and Map/Comprehension for details.
Examples A standard example is
rascal>{ x * x | int x <- [1 .. 10], x % 3 == 0 }
set[int]: {36,9,81}
i.e., the squares of the integers in the range [ 1 .. 10 ] that are divisible by 3. A more intriguing example (that we do not give in full detail) is
{name | /asgStat(Id name, _) <- P}
which traverses program P (using the descendant match operator /, see Patterns/Abstract) and constructs a set of all identifiers that occur on the left hand side of assignment statements in P.
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.