-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexceptby.go
103 lines (98 loc) · 3.44 KB
/
exceptby.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package go2linq
import (
"iter"
"slices"
"sort"
"sync"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [ExceptBy] produces the set difference of two sequences according to
// a specified key selector function and using [generichelper.DeepEqual] as key equaler.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [ExceptBy]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.exceptby
func ExceptBy[Source, Key any](first iter.Seq[Source], second iter.Seq[Key], keySelector func(Source) Key) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
r, err := ExceptByEq(first, second, keySelector, generichelper.DeepEqual[Key])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ExceptByEq] produces the set difference of two sequences according to
// a specified key selector function and using a specified key equaler.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [ExceptByEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.exceptby
func ExceptByEq[Source, Key any](first iter.Seq[Source], second iter.Seq[Key],
keySelector func(Source) Key, keyEqual func(Key, Key) bool) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if keyEqual == nil {
return nil, errorhelper.CallerError(ErrNilEqual)
}
return func(yield func(Source) bool) {
distinct1, _ := Distinct(first)
var once sync.Once
var distinct2 []Key
for s := range distinct1 {
once.Do(func() { deq2, _ := DistinctEq(second, keyEqual); distinct2 = slices.Collect(deq2) })
k := keySelector(s)
if !elInElelEq(k, distinct2, keyEqual) {
if !yield(s) {
return
}
}
}
},
nil
}
// [ExceptByCmp] produces the set difference of two sequences according to a specified
// key selector function and using a specified 'compare' to compare keys. (See [DistinctCmp].)
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [ExceptByCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.exceptby
func ExceptByCmp[Source, Key any](first iter.Seq[Source], second iter.Seq[Key],
keySelector func(Source) Key, compare func(Key, Key) int) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if compare == nil {
return nil, errorhelper.CallerError(ErrNilCompare)
}
return func(yield func(Source) bool) {
distinct1, _ := Distinct(first)
var once2 sync.Once
var distinct2 []Key
for s := range distinct1 {
once2.Do(func() {
deq2, _ := DistinctCmp(second, compare)
distinct2 = slices.Collect(deq2)
sort.Slice(distinct2, func(i, j int) bool { return compare(distinct2[i], distinct2[j]) < 0 })
})
k := keySelector(s)
if !elInElelCmp(k, distinct2, compare) {
if !yield(s) {
return
}
}
}
},
nil
}