-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
57 lines (48 loc) · 1.11 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
package itertools
import "github.com/anacrolix/missinggo"
type Iterator interface {
// Advances to the next value. Returns false if there are no more values.
// Must be called before the first value.
Next() bool
// Returns the current value. Should panic when the iterator is in an
// invalid state.
Value() interface{}
// Ceases iteration prematurely. This should occur implicitly if Next
// returns false.
Stop()
}
type sliceIterator struct {
slice []interface{}
value interface{}
ok bool
}
func (me *sliceIterator) Next() bool {
if len(me.slice) == 0 {
return false
}
me.value = me.slice[0]
me.slice = me.slice[1:]
me.ok = true
return true
}
func (me *sliceIterator) Value() interface{} {
if !me.ok {
panic("no value; call Next")
}
return me.value
}
func (me *sliceIterator) Stop() {}
func SliceIterator(a []interface{}) Iterator {
return &sliceIterator{
slice: a,
}
}
func StringIterator(a string) Iterator {
return SliceIterator(missinggo.ConvertToSliceOfEmptyInterface(a))
}
func IteratorAsSlice(it Iterator) (ret []interface{}) {
for it.Next() {
ret = append(ret, it.Value())
}
return
}