Range[T @ Num]
Describes a numeric interval for iteration and slicing.
Inherits from: Iterable[T]
Implements: Spread[T]
Constructor
Range …
Creates a range from start to end, counting by step.
a..b is half-open: it includes a and excludes b. An omitted endpoint
is nil.
Iteration counts from start, or 0 when it is nil, toward end by
step, which defaults to 1. A range whose end lies on the wrong side of
its start for the step is empty: 5..1 has no values, while
Range 5 1 -1 yields 5, 4, 3, 2. A range with no end iterates forever.
Iterating with a zero step raises ValueError.
Parameters
| Name | Type | Description |
|---|---|---|
start? |
(T | nil) |
|
end? |
(T | nil) |
|
step? |
(T | nil) |
Fields
end @ (T | nil)[get]The ending value (exclusive), or
nilfor open-end ranges.start @ (T | nil)[get]The starting value, or
nilfor open-start ranges.step @ (T | nil)[get]The step size, or
nilwhen unset.
Methods
(iter)() -> Iter[T]
Iterates the range's values.
(spread)() -> Spread[T]
Spreads the interval's values positionally.
contains value -> Bool
Tests whether the given value lies in the range: from start (inclusive)
toward end (exclusive) in the step's direction. With an explicit step,
the value must also be a whole number of steps from start. Without one,
any value in the interval matches, so ranges of any ordered type work.
Raises ValueError for a zero step.
Parameters
| Name | Type | Description |
|---|---|---|
value |
T |
the value to check |
Example
let r1 = Range 0 5
assert (r1.contains 0)
assert (r1.contains 4)
assert (!r1.contains 5)
let r2 = Range 5 0 -2
assert (r2.contains 5)
assert (r2.contains 1)
assert (!r2.contains 2)
# Without a step, the range ascends, and 5..0 is empty
assert (!(Range 5 0).contains 3)