Navigation
Synopsis A map comprehension generates a map value.
Syntax ( Exp1 : Exp2 | Gen1, Gen2, ... )
Types
Exp1 Exp2 ( Exp1 : Exp2 | Gen1, Gen2, ... )
T1 T2 map[T1, T2]
Description A map comprehension consists of a number of two contributing expressions Exp1 (for key values), and Exp2 (the values associated with those key values) and a number of generators Gen1, Gen2, Gen3, ... that are evaluated as described in Expressions/Comprehensions.
Examples Introduce a map of fruits:
rascal>fruits = ("pear" : 1, "apple" : 3, "banana" : 0, "berry" : 25, "orange": 35);
map[str, int]: ("banana":0,"pear":1,"orange":35,"berry":25,"apple":3)
rascal>import String;
ok
Use a map comprehension to filter fruits with a name of at most 5 characters:
rascal>(fruit : fruits[fruit] | fruit <- fruits, size(fruit) <= 5);
map[str, int]: ("pear":1,"berry":25,"apple":3)
Use a map comprehension to filter fruits with an associated value larger than 10:
rascal>(fruit : fruits[fruit] | fruit <- fruits, fruits[fruit] > 10);
map[str, int]: ("orange":35,"berry":25)
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.