-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
36 lines (31 loc) · 891 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
package iterator
// Iterator provides the required method Next
// for use in other iterator functions
type Iterator[TItem any] interface {
// Next returns the next item in the
// iterator, otherwise nil
Next() *TItem
}
// IntoIterator provides the Iter method used
// to obtain an iterator over a collection
type IntoIterator[TItem any] interface {
Iter() Iterator[TItem]
}
// From returns an iterator from an IntoIterator
func From[TItem any](into IntoIterator[TItem]) Iterator[TItem] {
return into.Iter()
}
// ForEachFunc represents the predicate function
// passed to ForEach
type ForEachFunc[TItem any] func(TItem)
// ForEach iterates over all items in the
// given iterator, passing each to the given
// predicate function
func ForEach[TItem any](
iter Iterator[TItem],
pred ForEachFunc[TItem],
) {
for val := iter.Next(); val != nil; val = iter.Next() {
pred(*val)
}
}