Range Expressions (0..5, 1..=10)

Range Expressions (0..5, 1..=10)

Range expressions let you describe a sequence of numbers concisely. They are the natural way to write counted loops in Luma.

Syntax

Luma supports two range operators:

Operator Name Example Values produced
.. Exclusive 0..5 0, 1, 2, 3, 4
..= Inclusive 0..=5 0, 1, 2, 3, 4, 5

The exclusive range .. stops before the end value. The inclusive range ..= includes the end value.

Using ranges with walk

Ranges are used with .walk() to iterate over a sequence of numbers.

Single parameter

The simplest form gives you one loop variable:

(0..5).walk(i) -> {
    print(i)
}
// Output: 0, 1, 2, 3, 4

Inclusive version:

(1..=5).walk(i) -> {
    print(i)
}
// Output: 1, 2, 3, 4, 5

Two parameters

With two parameters, the first is the index and the second is bound to the same value:

(0..=3).walk(index, value) -> {
    print("${index}: ${value}")
}
// Output:
// 0: 0
// 1: 1
// 2: 2
// 3: 3

Inline form

For simple bodies, use the inline arrow syntax:

(1..=5).walk(n) -> print(n)

Expressions as bounds

Both the start and end of a range can be any expression, not just literals:

start: int = 1
count: int = 10

(start..=count).walk(i) -> {
    print(i)
}

Loop control: skip and stop

Use skip to skip an iteration, or stop to exit the walk early:

(1..=20).walk(n) -> {
    if n % 2 == 0 { skip }
    if n > 10 { stop }
    print(n)
}
// Output: 1, 3, 5, 7, 9

See Walk Loops (skip & stop) for more details.

Common patterns

Counting from zero

(0..5).walk(i) -> print(i)
// 0, 1, 2, 3, 4

Counting from one

(1..=10).walk(n) -> print(n)
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Repeat N times

n: int = 3
(0..n).walk(i) -> {
    print("Attempt ${i + 1}")
}

Building a list

squares: [int] = []
(1..=5).walk(n) -> {
    squares.add(n * n)
}
print(squares)   // [1, 4, 9, 16, 25]

Quick reference

Expression Values Count
(0..5) 0, 1, 2, 3, 4 5
(0..=5) 0, 1, 2, 3, 4, 5 6
(1..4) 1, 2, 3 3
(1..=4) 1, 2, 3, 4 4
(0..0) (empty) 0
(0..=0) 0 1

Design notes

  • Ranges must be wrapped in parentheses when used with .walk(): (0..5).walk(...), not 0..5.walk(...)
  • The .. exclusive form matches the convention used in Rust, Kotlin, and Swift
  • The ..= inclusive form makes the intent explicit — no off-by-one confusion
Last updated on