Collect Expression
A collect expression builds a slice by repeatedly applying a function to a state and appending each resulting value to the slice.
A collect expression has the slice type whose element type is the first element of the function's return type.
Process
At runtime:
- the initial state expression—the expression following
over—is evaluated; - the function operand—the first expression—is evaluated;
- the function is applied to the current state;
- the resulting three-element tuple is decomposed into a result value, a next state, and a
bool; - the result value is appended to the slice;
- if the
boolisfalse, the iteration ends and the slice is returned; and - otherwise, the next state becomes the current state, and the process repeats.
The slice is allocated one element at a time, with each element constructed in place immediately following the preceding element (allocated contiguously); see region allocation.
Assertions
- The function operand has a function type with a single parameter whose type is the initial state's type.
- The function's return type is a three-element tuple type.
- The second element of the function's return type is the initial state's type.
- The third element of the function's return type is
bool. - The first element of the function's return type is runtime representable.
- The initial state expression's type is runtime representable.
Syntax
collect_expression = collect, unary_expression, over, unary_expression ;