-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
150 lines (127 loc) · 2.69 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
package goiter
type Iterator interface {
Next() bool
Value() interface{}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
type mapIter struct {
iterator Iterator
fn func(interface{}) interface{}
nv interface{}
}
func (mi *mapIter) Next() bool {
if mi.iterator.Next() {
mi.nv = mi.fn(mi.iterator.Value())
return true
}
return false
}
func (mi *mapIter) Value() interface{} {
return mi.nv
}
type filterIter struct {
iterator Iterator
fn func(interface{}) bool
nv interface{}
}
func (fi *filterIter) Next() bool {
if fi.iterator.Next() {
fi.nv = fi.iterator.Value()
if fi.fn(fi.nv) {
return true
} else {
return fi.Next()
}
}
return false
}
func (fi *filterIter) Value() interface{} {
return fi.nv
}
type whileIter struct {
iterator Iterator
fn func(interface{}) bool
nv interface{}
}
func (wi *whileIter) Next() bool {
if wi.iterator.Next() {
wi.nv = wi.iterator.Value()
if wi.fn(wi.nv) {
return true
}
}
return false
}
func (wi *whileIter) Value() interface{} {
return wi.nv
}
type skipIter struct {
iterator Iterator
skip uint
count uint
nv interface{}
}
func (si *skipIter) Next() bool {
if hasNext := si.iterator.Next(); hasNext {
si.nv = si.iterator.Value()
if si.count < si.skip {
si.count++
return si.Next()
}
si.count++
return hasNext
}
return false
}
func (si *skipIter) Value() interface{} {
return si.nv
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Consumer
func ForEach(iterator Iterator, f func(interface{})) {
for iterator.Next() {
f(iterator.Value())
}
}
// Consumer
func Collect(iterator Iterator) []interface{} {
var collected []interface{}
for iterator.Next() {
collected = append(collected, iterator.Value())
}
return collected
}
// Consumer
func Any(iterator Iterator, f func(interface{}) bool) bool {
for iterator.Next() {
if f(iterator.Value()) {
return true
}
}
return false
}
// Consumer
func All(iterator Iterator, f func(interface{}) bool) bool {
for iterator.Next() {
if !f(iterator.Value()) {
return false
}
}
return true
}
// Adaptor
func Map(iterator Iterator, f func(interface{}) interface{}) Iterator {
return &mapIter{iterator: iterator, fn: f}
}
// Adaptor
func Filter(iterator Iterator, f func(interface{}) bool) Iterator {
return &filterIter{iterator: iterator, fn: f}
}
// Adaptor
func While(iterator Iterator, f func(interface{}) bool) Iterator {
return &whileIter{iterator: iterator, fn: f}
}
// Adaptor
func Skip(iterator Iterator, skip uint) Iterator {
return &skipIter{iterator: iterator, skip: skip}
}