Range[optional A: (Real[A] val & (I8 val | I16 val | I32 val | I64 val | I128 val | ILong val | ISize val | U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val | F32 val | F64 val))]¶
Produces [min, max)
with a step of inc
for any Number
type.
// iterating with for-loop
for i in Range(0, 10) do
env.out.print(i.string())
end
// iterating over Range of U8 with while-loop
let range = Range[U8](5, 100, 5)
while range.has_next() do
try
handle_u8(range.next()?)
end
end
Supports min
being smaller than max
with negative inc
but only for signed integer types and floats:
var previous = 11
for left in Range[I64](10, -5, -1) do
if not (left < previous) then
error
end
previous = left
end
If inc
is nonzero, but cannot produce progress towards max
because of its sign, the Range
is considered empty and will not produce any iterations. The Range
is also empty if either min
equals max
, independent of the value of inc
, or if inc
is zero.
let empty_range1 = Range(0, 10, -1)
let empty_range2 = Range(0, 10, 0)
let empty_range3 = Range(10, 10)
empty_range1.is_empty() == true
empty_range2.is_empty() == true
empty_range3.is_empty() == true
Note that when using unsigned integers, a negative literal wraps around so while Range[ISize](0, 10, -1)
is empty as above, Range[USize](0, 10, -1)
produces a single value of min
or [0]
here.
When using Range
with floating point types (F32
and F64
) inc
steps < 1.0 are possible. If any arguments contains NaN, the Range
is considered empty. It is also empty if the lower bound min
or the step inc
are +Inf or -Inf. However, if only the upper bound max
is +Inf or -Inf and the step parameter inc
has the same sign, then the Range
is considered infinite and will iterate indefinitely.
let p_inf: F64 = F64.max_value() + F64.max_value()
let n_inf: F64 = -p_inf
let nan: F64 = F64(0) / F64(0)
let infinite_range1 = Range[F64](0, p_inf, 1)
let infinite_range2 = Range[F64](0, n_inf, -1_000_000)
infinite_range1.is_infinite() == true
infinite_range2.is_infinite() == true
for i in Range[F64](0.5, 100, nan) do
// will not be executed as `inc` is nan
end
for i in Range[F64](0.5, 100, p_inf) do
// will not be executed as `inc` is +Inf
end
class ref Range[optional A: (Real[A] val & (I8 val | I16 val | I32 val |
I64 val | I128 val | ILong val |
ISize val | U8 val | U16 val |
U32 val | U64 val | U128 val |
ULong val | USize val | F32 val |
F64 val))] is
Iterator[A] ref
Implements¶
- Iterator[A] ref
Constructors¶
create¶
Parameters¶
- min: A
- max: A
- inc: A = 1
Returns¶
- Range[A] ref^
Public Functions¶
has_next¶
Returns¶
- Bool val
next¶
Returns¶
- A ?
rewind¶
Returns¶
- None val
is_infinite¶
Returns¶
- Bool val
is_empty¶
Returns¶
- Bool val