-
Notifications
You must be signed in to change notification settings - Fork 1
/
except.go
64 lines (51 loc) · 1.39 KB
/
except.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
package flinx
import "github.com/kom0055/go-flinx/hashset"
// Except produces the set difference of two sequences. The set difference is
// the members of the first sequence that don't appear in the second sequence.
func Except[T comparable](q, q2 Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
next2 := q2.Iterate()
set := hashset.NewAny[T]()
for i, ok := next2(); ok; i, ok = next2() {
set.Insert(i)
}
return func() (item T, ok bool) {
for item, ok = next(); ok; item, ok = next() {
if !set.Has(item) {
return
}
}
return
}
},
}
}
// ExceptBy invokes a transform function on each element of a collection and
// produces the set difference of two sequences. The set difference is the
// members of the first sequence that don't appear in the second sequence.
func ExceptBy[T any, V comparable](selector func(T) V) func(q, q2 Query[T]) Query[T] {
return func(q, q2 Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
next2 := q2.Iterate()
set := hashset.NewAny[V]()
for i, ok := next2(); ok; i, ok = next2() {
s := selector(i)
set.Insert(s)
}
return func() (item T, ok bool) {
for item, ok = next(); ok; item, ok = next() {
s := selector(item)
if !set.Has(s) {
return
}
}
return
}
},
}
}
}