-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
114 lines (101 loc) · 2 KB
/
iter.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
package iter
type IteratorObj interface {
HasNext() bool
Next() int
}
type accumulateIterator struct {
index int
length int
current int
data *[]int
f func(int, int) int
}
// 两位二元函数计算迭代
func (a *accumulateIterator) HasNext() bool { return a.index < a.length }
func (a *accumulateIterator) Next() int {
a.current = a.f(a.current, (*a.data)[a.index])
a.index++
return a.current
}
func Accumulate(i *[]int, f ...func(a, b int) int) IteratorObj {
a := &accumulateIterator{
data: i,
length: len(*i),
index: 0,
current: 0}
if len(f) > 0 {
a.f = f[0]
} else {
a.f = func(i1, i2 int) int { return i1 + i2 }
}
return a
}
func Chain() {
}
func Combinations() {}
func CombinationsWithReplacement() {}
func Compress() {}
// 无限计数迭代
type countIterator struct {
start int
step int
}
func (c *countIterator) HasNext() bool { return true }
func (c *countIterator) Next() int {
result := c.start
c.start += c.step
return result
}
func Count(start int, step ...int) IteratorObj {
c := &countIterator{
start: 0,
step: 1,
}
if len(step) > 0 {
c.step = step[0]
}
return c
}
type cycleIterator struct {
data *[]int
length int
index int
repeatNum int
repeat int
hasRepeat bool
}
func (c *cycleIterator) HasNext() bool {
return c.repeatNum < c.repeat
}
func (c *cycleIterator) Next() int {
result := (*c.data)[c.index]
c.index = (c.index + 1) % c.length
if c.index == 0 && c.hasRepeat {
c.repeatNum += 1
}
return result
}
func Cycle(i *[]int, repeat ...int) IteratorObj {
c := &cycleIterator{
data: i, hasRepeat: false, length: len(*i),
}
if len(repeat) > 0 {
c.hasRepeat = true
c.repeat = repeat[0]
}
return c
}
func Dropwhile() {}
func FilterFalse() {}
type groupByIterator struct {
}
func GroupBy() {
}
func Islice() {}
func Permutations() {}
func Product() {}
func Repeat() {}
func Starmap() {}
func Takewhile() {}
func Tee() {}
func ZipLongest() {}