Range Types
Why This Matters
Ranges are Arden's core numeric iteration primitive. You use them in loops, manual iteration, and simple numeric traversal without building lists first.
Quick Mental Model
range(start, end)-> values fromstartup toend(end-exclusive)- optional third arg is
step - works with
IntegerandFloat - step must be non-zero
Constructing Ranges
import std.io.*;
function main(): None {
r1: Range<Integer> = range(0, 5);
r2: Range<Integer> = range(0, 10, 2);
rf: Range<Float> = range(0.0, 1.0, 0.25);
println("constructed ranges");
return None;
}
Manual Iteration (Runnable)
import std.io.*;
function main(): None {
r: Range<Integer> = range(0, 5);
while (r.has_next()) {
value: Integer = r.next();
println("value={value}");
}
return None;
}
Iteration contract:
has_next()tells you whether the current position is within configured boundsnext()advances and returns current valuenext()itself does not enforce bounds; calling it afterhas_next() == falsecontinues arithmetic progression
Example behavior:
range(0, 1)then repeatednext()yields0,1,2, ...- therefore, always gate
next()withhas_next()in bounded loops
In for Loops
import std.io.*;
function main(): None {
for (i in 5) {
println("i={i}");
}
return None;
}
for (i in 5) is shorthand numeric iteration and is often the cleanest option.
Descending numeric traversal:
import std.io.*;
function main(): None {
r: Range<Integer> = range(10, 0, -2);
while (r.has_next()) {
println("v={r.next()}");
}
return None;
}
Common Mistakes
- assuming end is included (it is excluded)
- using zero step (
range(0, 10, 0)) which is invalid - mismatching numeric kinds across arguments
- using wrong step direction (
range(0, 10, -1)starts withhas_next=false) - calling
next()afterhas_next()is alreadyfalse
Related
- Control Flow
- Example:
25_range_types