Iterator
From the SETL Wiki
An iterator is a construct that extracts values from a set, tuple or string for use in a loop or comprehension.
There are three types of iterator in SETL:
- General iterator: x in s. Assigns one of the following to x:
- each member of the set s,
- each element of the tuple s or
- each character of the string s.
- smap iterator: v = s(k). Assigns one of the following to k and v:
- each key and unique corresponding value of the map s,
- each index and element of the tuple s or
- each index and character of the string s.
- mmap iterator: v = s{k}. Assigns to k and v:
- each key and image set of the multi-valued map s.
Iterators are similar to clauses in a logic programming language such as Prolog, as they have the same form as certain types of relational expression and cause all possible combinations of values to be assigned to the bound variables that would make the corresponding relational expression true, e.g. in x in [1, 2, 3] All three values (1, 2, 3) are assigned to x and this is exactly the set of values for which the boolean expression x in [1, 2, 3] yields true.
There is a slight difference for strings in that only single characters are assigned to x, not all substrings. e.g. x in 'ab' assigns 'a' and 'b' to x but not 'ab', even though 'ab' in 'ab' is true.
Iterators can appear in two places in a SETL program:
- In a for clause in a loop, e.g.
(for x in {1, 2, 3}) print(x); end;
- In a comprehension, e.g.
print([x * x: x in [1, 2, 3]]);
The second case also introduces an (implicit) loop into the program.
Iterators can be nested by joining them with commas, e.g.
[x * 10 + y: x in [1, 2], y in [4, 5]]
yields [14, 15, 24, 25].
Iterators can also have a guard condition added. This is a boolean-valued expression used to filter the extracted values. The body of the loop will be skipped (or in a comprehension the value will be omitted from the result) if the guard expression evaluates to false. e.g.
[x * x: x in [19 .. 33] | '2' notin str x]
returns [361 900 961 1089] (the squares of those integers between 19 and 33 inclusive, whose decimal expansions do not contain the digit '2'.)

