-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.go
57 lines (47 loc) · 877 Bytes
/
range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package trompe
import (
"fmt"
)
type Range struct {
Start int
End int
Close bool
}
func NewRange(start int, end int, close bool) *Range {
return &Range{start, end, close}
}
func (r *Range) Type() int {
return ValueTypeRange
}
func (r *Range) Desc() string {
if r.Close {
return fmt.Sprintf("%d..<%d", r.Start, r.End)
} else {
return fmt.Sprintf("%d...%d", r.Start, r.End)
}
}
type RangeIter struct {
cur int
end int
}
func (r *Range) NewIter() Iter {
if r.Close {
return &RangeIter{r.Start, r.End}
} else {
return &RangeIter{r.Start, r.End - 1}
}
}
func (iter *RangeIter) Type() int {
return ValueTypeIter
}
func (iter *RangeIter) Desc() string {
return fmt.Sprintf("Iter(%d...%d)", iter.cur, iter.end)
}
func (iter *RangeIter) Next() Value {
if iter.cur <= iter.end {
iter.cur++
return NewInt(iter.cur - 1)
} else {
return nil
}
}