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 handle_u8(range.next()) 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 the step is not moving min towards max or if it is 0, the Range is considered infinite and iterating over it will never terminate:
let infinite_range1 = Range(0, 1, 0)
infinite_range1.is_infinite() == true
let infinite_range2 = Range[I8](0, 10, -1)
for _ in infinite_range2 do
env.out.print("will this ever end?")
env.err.print("no, never!")
end
When using Range with floating point types (F32 and F64) inc steps < 1.0 are possible. If any of the arguments contains NaN, +Inf or -Inf the range is considered infinite as operations on any of them won't move min towards max. The actual values produced by such a Range are determined by what IEEE 754 defines as the result of min + inc:
for and_a_half in Range[F64](0.5, 100) do handle_half(and_a_half) end // this Range will produce 0 at first, then infinitely NaN let nan: F64 = F64(0) / F64(0) for what_am_i in Range[F64](0, 1000, nan) do wild_guess(what_am_i) 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
new ref create( min: A, max: A, inc: A = 1) : Range[A] ref^
fun box has_next() : Bool val
fun ref next() : A
fun ref rewind() : None val
fun box is_infinite() : Bool val
© 2016-2018, The Pony Developers
© 2014-2015, Causality Ltd.
Licensed under the BSD 2-Clause License.
https://stdlib.ponylang.io/collections-Range