Skip to content

Range[T @ Num]

Describes a numeric interval for iteration and slicing.

Inherits from: Iterable[T]

Constructor

Range …

let bounded = (1..5)
let from_start = (..5)
let to_end = (1..)
let all = (..)

a..b is half-open: it includes a and excludes b.

Open-ended forms keep the missing endpoint as nil internally.

Parameters

NameTypeDescription
start? T
end? T
step? T

Methods

(iter)() -> Iter[T]

Iterates the interval. Open-ended ranges require a concrete start.

contains value -> Bool

Tests whether the given value is within the Range interval. For increasing ranges (start < end), checks if value is in [start, end). For decreasing ranges (start > end), checks if value is in (end, start].

Parameters

NameTypeDescription
value T the value to check

Example

# Increasing Range [0, 5)
let r1 = Range 0 5
assert (r1.contains 0)
assert (r1.contains 4)
assert (!r1.contains 5)

# Decreasing Range (0, 5]
let r2 = Range 5 0 -1
assert (r2.contains 5)
assert (r2.contains 1)
assert (!r2.contains 0)
Open in playground

end() -> (T | nil)

The ending value (exclusive), or nil for open-end ranges.

start() -> (T | nil)

The starting value, or nil for open-start ranges.

step() -> (T | nil)

The step size, or nil when unset.