Iteration

"for" loops

The usual way to iterate a collection is with a for loop.

main void() a nat[] = 1, 2, 3 for x in a log x

A for loop is an expression. It can be void, or return an output collection.

main void() a nat[] = 1, 2, 3 b nat[] = for x in a x * 10 log b

A type annotation after the for keyword types the entire for expression.

main void() a nat[] = 1, 2, 3 log for::nat[] x in a x * 10

A for loop can be squeezed onto one line using then.

main void() a nat[] = 1, 2, 3 log for::nat[] x in a then x * 10

for loops are just a convenient syntax for calling a function named for-loop with a lambda.

main void() a nat[] = 1, 2, 3 log for-loop::nat[] a, of x then x * 10

There are also for* loops, which work like for loops, but the body returns a collection that is flattened into the result.
These call a function named for-star.

main void() a nat[] = 1, 2, 3 log for*::nat[] x in a x, x

Iteration with early return

There is no early return from a for loop; it always iterates every element of the collection.

The most primitive iteration function is some which stops when the lambda returns true.

main void() a nat[] = 1, 2, 3 log some a, of x log "checking {x}" is-even x

A more general function is first. Here, the callback returns an option, and the first non-empty result is returned.

main void() a nat[] = 1, 2, 3 log first a, of x log "checking {x}" if is-even x x * 10

first works a lot like an until loop with a condition-body (see Loops); it keeps running the body until it gets a result.
Unlike an until loop, it can also stop running if it reaches the end of the collection, so it returns an option.