Ranges
Range expressions produce arrays of integers from some start up to (or down to) some limit:
def ints = 1..<10
> ints
[1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32]
In the expression above, the upper limit is not included. Inclusive ranges are also supported:
def ints_inclusive = 1...10
> ints_inclusive
[1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32, 10i32]
By default the distance between each number is 1 (the stride). This can be changed by providing an explicit second element:
def ints_by_two = 1..3..<10
> ints_by_two
[1i32, 3i32, 5i32, 7i32, 9i32]
If we want a descending range, we must provide a second element:
def ints_descending = 10..9...0
> ints_descending
[10i32, 9i32, 8i32, 7i32, 6i32, 5i32, 4i32, 3i32, 2i32, 1i32, 0i32]
An exclusive descending range is also possible:
def ints_descending_exclusive = 10..9..>0
> ints_descending_exclusive
[10i32, 9i32, 8i32, 7i32, 6i32, 5i32, 4i32, 3i32, 2i32, 1i32]
Hints
The predefined functions iota
and indices
encapsulate common
usages of ranges.
Instead of 0..<x
, use iota x
.
Instead of iota (length A)
(or 0..<length A
), use indices A
.