-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
56 lines (47 loc) · 822 Bytes
/
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
package stream
type iterator[T any] interface {
copyInto(sink[T])
}
type sliceIterator[T any] []T
func (it sliceIterator[T]) copyInto(s sink[T]) {
s.begin()
for _, v := range it {
if !s.done() {
s.accept(v)
} else {
break
}
}
s.end()
}
type generatorIterator[T any] func() T
func (it generatorIterator[T]) copyInto(s sink[T]) {
s.begin()
for !s.done() {
s.accept(it())
}
s.end()
}
type seedIterator[T any] struct {
x T
operator func(T) T
}
func (it *seedIterator[T]) copyInto(s sink[T]) {
s.begin()
for !s.done() {
s.accept(it.x)
it.x = it.operator(it.x)
}
s.end()
}
type whileIterator[T any] struct {
hasNext func() bool
next func() T
}
func (it *whileIterator[T]) copyInto(s sink[T]) {
s.begin()
for !s.done() && it.hasNext() {
s.accept(it.next())
}
s.end()
}