Additionally to the structural language elements described so far,
there are special commands that apply to lists
and
maps
. What is special about these commands is that
they apply an expression to the single elements of a list or
map. This is done in a functional manner,
i.e. the expression to be applied is passed as a parameter. Generally
this executes faster than a procedural loop
because the internal functionality is realized in a very effective
way.
Furthermore some of these commands create lists from maps, maps from maps, maps from lists etc., so that they can be used to avoid the cumbersome assembling of these compound data types in a procedural loop.
Synopsis (list): any
foreach (
type variable, list<type>, { expression }
);
Synopsis (map): any
foreach(
type_key variable_key, type_value variable_value,
map<type_key, type_value>, { expression }
);
This statement is a means to process the content of list or map in a sequential manner. It establishes an implicit loop over all entries of the list or map thereby executing the given expression with the respective entries. With lists the variable is a placeholder for the current entry. With maps, variable_key and variable_value are substituted for the respective key-value-pair.
![]() | Note |
---|---|
The return value of the last execution of expression determines the value of the whole foreach() statement. |
Example 8.23. foreach() Loop
{ // Exemplary foreach for list // This yields 3 foreach(integer value, [1, 2, 3], { return value; }); // Exemplary foreach for map // This yields 9 foreach(integer key, integer value, $[1:1, 2:4, 3:9], { y2milestone("value: %1", value); return value; }); }
Example 8.24. Sophisticated foreach() Loop
{ list <string> codes = ["X1", "D", "vT", "o", "T5h8"]; list <string> one_letter_codes = []; list <string> other_codes = []; foreach (string code, codes, { // all one-letter codes if (size(code) == 1) { one_letter_codes = add (one_letter_codes, code); // other ones } else { other_codes = add (other_codes, code); } }); // Results in: ["D", "o"] y2milestone("One-letter codes: %1", one_letter_codes); // Results in: ["X1", "vT", "T5h8"] y2milestone("Other codes: %1", other_codes); }
Synopsis: map<type1, type2>
listmap(
type3 variable, list<type3>, { expression returning
map<type1, type2> }
);
This statement is a means to process the content of list in a sequential manner. It establishes an implicit loop over all entries in list thereby executing the given expression with the respective entry. During execution variable is a placeholder for the current entry. For each element in list the expression is evaluated in a new context. The result of each evaluation MUST be a map with a single pair of key-value. All the returned key-value-pairs are assembled to form the new map that is returned.
Example 8.25. listmap() statement
{ // This results in $[1:"xy", 2:"xy", 3:"xy"] map <integer, string> m1 = listmap (integer s, [1, 2, 3], { return $[s: "xy"] }); // This results in $[11:2, 12:4, 13:6] map <integer, integer> m2 = listmap (integer s, [1, 2, 3], { integer a = s+10; integer b = s*2; list ret = [a, b]; return ret; }); y2milestone("map 1: %1 - map 2: %2", m1, m2); }
Synopsis (map): list<type1>
maplist(
type2 key, type3 value, map<type2, type3>,
{ block returning type1 }
);
Synopsis (list): list<type1>
maplist(
type2 variable, list<type2>,
{ block returning type1 }
);
This statement is a means to process the content of map or list in a sequential manner. It establishes an implicit loop over all entries in map or list thereby executing the given expression with the respective entries. With lists the variable is a placeholder for the current entry. With maps, key and value are substituted for the respective key-value-pair. For each element the expression is evaluated in a new context. All return values are assembled to form the new list that is returned.
![]() | Note |
---|---|
To exit the loop before it ends, use the break. See the usage in the break statement description. To skip to the next loop step, use the continue. See the usage in the continue statement description. |
Synopsis: map<type1, type2>
mapmap(
type3 key, type4 value, map<type3, type4>,
{ expression returning map<type1, type2> }
);
This statement is a means to process the content of map in a sequential manner. It establishes an implicit loop over all entries in map thereby executing the given expression with key and value substituted for the respective key-value-pair. For each map element the expression is evaluated in a new context. The result of each evaluation MUST be a map with a single pair of key-value. All the returned key-value-pairs are assembled to form the new map that is returned.