Numeric ranges
Factor handbook > The language > Collections > Sequence operations

Prev:Counted loops
Next:Control flow with sequences


A range is a virtual sequence with real number elements ranging from a to b by step. Ascending as well as descending ranges are supported.

The class of ranges:
range


Creating ranges with integer end-points. The standard mathematical convention is used, where ( or ) denotes that the end-point itself is not part of the range; [ or ] denotes that the end-point is part of the range:
[a,b] ( a b -- range )

(a,b] ( a b -- range )

[a,b) ( a b -- range )

(a,b) ( a b -- range )

[0,b] ( b -- range )

[1,b] ( b -- range )

[0,b) ( b -- range )


Creating general ranges:
<range> ( a b step -- range )


Ranges are most frequently used with sequence combinators as a means of iterating over integers. For example,
3 10 [a,b] [ sqrt ] map

Computing the factorial of 100 with a descending range:
100 1 [a,b] product

A range can be converted into a concrete sequence using a word such as >array. In most cases this is unnecessary since ranges implement the sequence protocol already. It is necessary if a mutable sequence is needed, for use with words such as set-nth or map!.