"For Each" Loop Statement

Unlike C, C++, Java and most languages, OTScript does not provide imperative loops. Instead, it provides a "for each" loop with a compact syntax. The syntax is based on a . placed between expressions.

This page discusses:

See Also
Operators

Syntax



Use

The instruction evaluates the right block for each value resulting from the left expression, and builds a list corresponding to the results of all iterations.

The syntax .{ excepts at least one loop.

The loop syntax .+{ is the same as .{ except that the instructions are executed even if there is no value to iterate. In which case, each variable is bound to an empty value.

Example1

// Like OCaml's List.map or JavaScript Array.map
TMP allReqPriorities := getAllRequirements.(Title + "|" + revision + "|" + Priority);

Example2

// Like imperative loop
TMP fibonacci := 0 $+ 1; // predefines fib(0) and fib(1)
$INTERVAL(2 $+ 10).{ // computes from fib(2) to fib(10)
fibonacci += $AT(fibonacci, -1) + $AT(fibonacci, -2)
};

Example3

// Like List.map but using instruction block instead of simple expression
OPENBUFF;
getAllRequirements.{
PUTF("* $1 $2"n", Title, revision);
PUTN("Description:");
PUTN(`Content Text`);
// in this case, the result of the stream operation is irrelevant
};
TMP allReqsAsText := CLOSE;