-
Notifications
You must be signed in to change notification settings - Fork 79
/
iterator.go
45 lines (35 loc) · 1006 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
package behavioral
// Iterator is an interface for an iterator.
type Iterator interface {
// Index returns the index of the current iterator.
Index() int
// Value returns the current value of the iterator.
Value() interface{}
// HasNext returns whether another next element exists.
HasNext() bool
// Next increments the iterator to point to the next element.
Next()
}
// ArrayIterator is an iterator which iterates over an array.
type ArrayIterator struct {
array []interface{}
index int
}
// Index returns the index of the current iterator.
func (i *ArrayIterator) Index() int {
return i.index
}
// Value returns the current value of the iterator.
func (i *ArrayIterator) Value() interface{} {
return i.array[i.index]
}
// HasNext returns whether another next element exists.
func (i *ArrayIterator) HasNext() bool {
return i.index+1 != len(i.array)
}
// Next increments the iterator to point to the next element.
func (i *ArrayIterator) Next() {
if i.HasNext() {
i.index++
}
}