Counted loops
Factor handbook » The language » Collections » Sequence operations

Prev:Deep sequence combinators
Next:Numeric ranges


A virtual sequence is defined for iterating over integers from zero.
<iota> ( n -- iota )

For example, calling <iota> on the integer 3 produces a sequence containing the elements 0, 1, and 2. This is very useful for performing counted loops using words such as each:
USING: sequences prettyprint ; 3 <iota> [ . ] each
0 1 2

A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using each-index, map-index and reduce-index.

Combinators that produce new sequences, such as map, will output an array if the input is an instance of <iota>.

More elaborate counted loops can be performed with Numeric ranges.