-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.go
43 lines (33 loc) · 810 Bytes
/
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
package set
import "github.com/Jamlie/set/internal"
type setIter[T comparable] struct {
set *Set[T]
internalSet *Set[T]
}
func (it *setIter[T]) Map(fn internal.MapIterFn[T]) *setIter[T] {
newSet := New[T]()
for k := range it.internalSet.set {
newKey := fn(k)
newSet.set[newKey] = struct{}{}
}
it.internalSet.set = newSet.set
return it.internalSet.Iter()
}
func (it *setIter[T]) Filter(fn internal.FilterIterFn[T]) *setIter[T] {
newSet := New[T]()
for k := range it.internalSet.set {
if fn(k) {
newSet.set[k] = struct{}{}
}
}
it.internalSet.set = newSet.set
return it.internalSet.Iter()
}
func (it *setIter[T]) ForEach(fn internal.ForEachIterFn[T]) {
for k := range it.internalSet.set {
fn(k)
}
}
func (it *setIter[T]) Collect() {
it.set.set = it.internalSet.set
}