-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
54 lines (48 loc) · 1.06 KB
/
filter.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
package gcf
type filterIterable[T any] struct {
itb Iterable[T]
filterFunc func(v T) bool
}
type filterIterator[T any] struct {
it Iterator[T]
filterFunc func(v T) bool
iteratorItem[T]
}
// Filter makes Iterable with elements which filterFunc is true.
//
// itb := gcf.FromSlice([]int{1, 2, 3})
// itb = gcf.Filter(itb, func(v int) bool { return v%2 > 0 })
//
// If filterFunc is nil, returns original Iteratable.
func Filter[T any](itb Iterable[T], filterFunc func(v T) bool) Iterable[T] {
if isEmpty(itb) {
return orEmpty(itb)
}
if filterFunc == nil {
return itb
}
return &filterIterable[T]{itb, filterFunc}
}
func (itb *filterIterable[T]) Iterator() Iterator[T] {
return &filterIterator[T]{
it: itb.itb.Iterator(),
filterFunc: itb.filterFunc,
}
}
func (it *filterIterator[T]) MoveNext() bool {
if it.done {
return false
}
for it.it.MoveNext() {
c := it.it.Current()
if it.filterFunc(c) {
it.current = c
return true
}
}
it.MarkDone()
return false
}
func (it *filterIterator[T]) Current() T {
return it.current
}