-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
171 lines (132 loc) · 2.8 KB
/
iterator.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package native
type State = int
const (
NEXT State = iota + 1
SKIP
FINISH
)
type MapContainer struct {
data map[string]bool
Next func(index string, value bool) (string, bool, State)
}
func MapIterator(data map[string]bool) *MapContainer {
return &MapContainer{
data: data,
Next: func(index string, value bool) (string, bool, State) {
return index, value, NEXT
},
}
}
func (o *MapContainer) Filter(f func(string, bool) bool) *MapContainer {
next := o.Next
o.Next = func(i string, v bool) (index string, value bool, state State) {
if index, value, state = next(i, v); state == NEXT && !f(index, value) {
state = SKIP
}
return
}
return o
}
func (o *MapContainer) Take(count int) *MapContainer {
next := o.Next
counter := 0
o.Next = func(i string, v bool) (index string, value bool, state State) {
if index, value, state = next(i, v); state == NEXT {
if counter >= count {
state = FINISH
}
counter++
}
return
}
return o
}
func (o *MapContainer) Range(fn func(index string, value bool)) {
for index, value := range o.data {
switch index, value, status := o.Next(index, value); status {
case SKIP:
continue
case FINISH:
return
case NEXT:
fn(index, value)
}
}
}
func (o *MapContainer) Len() (count int) {
o.Range(func(_ string, _ bool) {
count++
})
return
}
type UintContainer struct {
data []uint
Next func(index int, value uint) (int, uint, State)
}
func UintIterator(data []uint) *UintContainer {
return &UintContainer{
data: data,
Next: func(index int, value uint) (int, uint, State) {
return index, value, NEXT
},
}
}
func (o *UintContainer) Filter(f func(int, uint) bool) *UintContainer {
next := o.Next
o.Next = func(i int, v uint) (index int, value uint, state State) {
if index, value, state = next(i, v); state == NEXT && !f(index, value) {
state = SKIP
}
return
}
return o
}
func (o *UintContainer) Take(count int) *UintContainer {
next := o.Next
counter := 0
o.Next = func(i int, v uint) (index int, value uint, state State) {
if index, value, state = next(i, v); state == NEXT {
if counter >= count {
state = FINISH
}
counter++
}
return
}
return o
}
func (o *UintContainer) Range(fn func(index int, value uint)) {
for index, value := range o.data {
switch index, value, status := o.Next(index, value); status {
case SKIP:
continue
case FINISH:
return
case NEXT:
fn(index, value)
}
}
}
func (o *UintContainer) Mul() (mul uint) {
mul = 1
o.Range(func(index int, value uint) {
mul *= value
})
return
}
func (o *UintContainer) Len() (count int) {
o.Range(func(_ int, _ uint) {
count++
})
return
}
func Range(begin, end int) <-chan int {
yield := make(chan int)
go func() {
for ; begin < end; begin++ {
yield <- begin
}
close(yield)
}()
return yield
}