-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
73 lines (63 loc) · 2.42 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
/*
Package iter defines iterable and iterator interfaces -- protocols really.
Iterators are clunkier and likely slower than for-range statements, so they
aren't for iterating over slices, maps, or channels. Rather, they're most useful
in contexts where go does not provide an obvious way to iterate.
This package provides examples of the interface for elements of type interface{},
int, and string.
*/
package iter
import "errors"
// EmptyIteratorError is optionally the error return value for an iterator's
// Next method when its HasNext method returns false because the iterator is
// empty.
var EmptyIteratorError error = errors.New("iter: empty iterator")
// Iterable containers allow iteration over their contents with incantations
// like
// for it := container.Iter(); it.HasNext(); {
// item, err := it.Next()
// // operations with item
// }
// Not all iterators will use the error return value, and you can safely ignore
// the error return value in those cases. Others iterators, such as ones that
// iterate over files, may use them to indicate problems in the underlying
// data. Make sure HasNext returns true before calling Next.
type Iterable interface {
// Iter returns a new Iterator instance starting from a position independant
// of the state of any other Iterators.
Iter() Iterator
}
// Iterators are made with an Iterable's Iter method. Then the caller calls Next
// as long as HasNext returns true. The behavior of an iterator is undefined
// when the underlying container is modified after Iter returns the iterator
// (except that calling an iterator's Iter method returns the receiver
// instance).
type Iterator interface {
// Iter returns the receiver.
Iterable
// HasNext returns false if the iterator is empty, guaranteeing that Next's
// error return value is not EmptyIteratorError.
HasNext() bool
// Next returns the next item in the iterator if HasNext returns true and
// the return error value is nil. If HasNext returns false, Next may return
// EmptyIteratorError as the error, but the main return value is undefined.
Next() (interface{}, error)
}
// Iteratable over int elements.
type IntIterable interface {
Iter() IntIterator
}
type IntIterator interface {
IntIterable
HasNext() bool
Next() (int, error)
}
// Iteratable over string elements.
type StringIterable interface {
Iter() StringIterator
}
type StringIterator interface {
StringIterable
HasNext() bool
Next() (string, error)
}