Numeric ranges

Factor handbook » The language » Collections » Sequence operations

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,

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!.
This documentation was generated offline from a
`load-all`

image. If you want, you can also
browse the documentation from within the UI developer tools. See
the Factor website
for more information.

Factor 0.99 x86.64 (2190, heads/master-0c89831e8b, Jan 30 2023 23:29:08)