1 package frost.core
2
3 ====================================================================================================
4 Similar to [Range], but with a `step` value that indicates the number of steps to take during each
5 iteration. For instance, the code:
6
7 -- testcase SteppedRangeIntro1(Simple)
8 for i in SteppedRange<Int, Int>(0, 100, 10, true) {
9 Console.printLine(i)
10 }
11
12 will count by 10 and print the numbers 0, 10, 20, ..., 90, 100. The exclusive range (`..`) and
13 inclusive range (`...`) operators provide a shorthand syntax for creating `Range` and `SteppedRange`
14 objects; the loop above could be more succinctly (and readably) written:
15
16 -- testcase SteppedRangeIntro2(Simple)
17 for i in 0 ... 100 by 10 {
18 Console.printLine(i)
19 }
20 ====================================================================================================
21 @specialize
22 class SteppedRange<EndPoint:Value?, Step:Value> : Value {
23 ================================================================================================
24 The range's starting point.
25 ================================================================================================
26 def start:EndPoint
27
28 ================================================================================================
29 The range's ending point.
30 ================================================================================================
31 def end:EndPoint
32
33 ================================================================================================
34 The range's step count.
35 ================================================================================================
36 def step:Step
37
38 ================================================================================================
39 `true` if the range includes its endpoint.
40 ================================================================================================
41 def inclusive:Bit
42
43 ================================================================================================
44 Creates a new `SteppedRange`.
45 ================================================================================================
46 init(start:EndPoint, end:EndPoint, step:Step, inclusive:Bit) {
47 self.start := start
48 self.end := end
49 self.step := step
50 self.inclusive := inclusive
51 }
52
53 @override
54 function get_toString():String {
55 def result := MutableString()
56 if start !== null {
57 result.append(start!)
58 }
59 if inclusive {
60 result.append("...")
61 }
62 else {
63 result.append("..")
64 }
65 if end !== null {
66 result.append(end!)
67 }
68 result.append(" by ")
69 result.append(step)
70 return result.finish()
71 }
72 }