-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunionby.go
72 lines (67 loc) · 2.37 KB
/
unionby.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [UnionBy] produces the set union of two sequences according to
// a specified key selector function and using [generichelper.DeepEqual] as key equaler.
//
// [UnionBy]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
func UnionBy[Source, Key any](first, second iter.Seq[Source], 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 := UnionByEq(first, second, keySelector, generichelper.DeepEqual[Key])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [UnionByEq] produces the set union of two sequences according to
// a specified key selector function and using a specified key equaler.
//
// [UnionByEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
func UnionByEq[Source, Key any](first, second iter.Seq[Source],
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)
}
concat, _ := Concat(first, second)
r, err := DistinctByEq(concat, keySelector, keyEqual)
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [UnionByCmp] produces the set union of two sequences according to a specified
// key selector function and using a specified key comparer. (See [DistinctCmp].)
//
// [UnionByCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
func UnionByCmp[Source, Key any](first, second iter.Seq[Source],
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)
}
concat, _ := Concat(first, second)
r, err := DistinctByCmp(concat, keySelector, compare)
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}