Reduce generated values to a single value.
( InitExp | RedExp | Gen₁, Gen₂, ... )
Every reducer (as above) is equivalent to the following code:
it = _InitExp_; ❶
for(_Gen₁_, _Gen₂_, ... ) ❷
it = _RedExp_; ❸
and is executed as follows:
- A fresh variable, always named
it, is initialized with InitExp. We call the variable
itsince we use
itto initialize the reducer, to make changes to
it, and to return
- A for loop iterates over all values produced by the generators
Gen₂, ... .
- In the body of the loop, variable
itis updated to reflect a new reduced value. Note that
ititself and variables introduced in Gen₁, Gen₂, ... may occur in RedExp.
- The final value of
itis the result of the reducer.
rascal>L = [1, 3, 5, 7];
rascal>(0 | it + e | int e <- L);
rascal>(1 | it * e | int e <- L);
- A reducer resembles the fold function found in most functional languages.