Such that predicatesΒΆ

A for loop can be followed by a “|” and then a predicate. The predicate qualifies the use of the values from the iterator that follows the for. Think of the vertical bar “|” as the phrase “such that”.

for n in 0..4 | odd? n repeat output n
 1
 3
                    Type: Void

This loop expression prints out the integers n in the given segment such that n is odd.

A for loop can also be written

for iterator | predicate repeat loopbody

which is equivalent to:

for iterator repeat if predicate then loopbody else iterate

The predicate need not refer only to the variable in the for clause. Any variable in an outer scope can be part of the predicate.

for i in 1..50 repeat
  for j in 1..50 | factorial(i+j) < 25 repeat
    output [i,j]
 [1,1]
 [1,2]
 [1,3]
 [2,1]
 [2,2]
 [3,1]
                    Type: Void

Table Of Contents

This Page