Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:

  1. the initial state expression—the expression following over—is evaluated;
  2. the function operand—the first expression—is evaluated;
  3. the function is applied to the current state;
  4. the resulting three-element tuple is decomposed into a result value, a next state, and a bool;
  5. the result value is appended to the slice;
  6. if the bool is false, the iteration ends and the slice is returned; and
  7. 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

  1. The function operand has a function type with a single parameter whose type is the initial state's type.
  2. The function's return type is a three-element tuple type.
  3. The second element of the function's return type is the initial state's type.
  4. The third element of the function's return type is bool.
  5. The first element of the function's return type is runtime representable.
  6. The initial state expression's type is runtime representable.

Syntax

collect_expression = collect, unary_expression, over, unary_expression ;