-
Notifications
You must be signed in to change notification settings - Fork 6
/
unionby.go
73 lines (65 loc) · 2.95 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
73
package go2linq
import (
"github.com/solsw/collate"
"github.com/solsw/errorhelper"
)
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
// [UnionBy] produces the set union of two sequences according to
// a specified key selector function and using [collate.DeepEqualer] as key equaler.
//
// [UnionBy]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
func UnionBy[Source, Key any](first, second Enumerable[Source], keySelector func(Source) Key) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return UnionByEq(first, second, keySelector, nil)
}
// UnionByMust is like [UnionBy] but panics in case of error.
func UnionByMust[Source, Key any](first, second Enumerable[Source], keySelector func(Source) Key) Enumerable[Source] {
return errorhelper.Must(UnionBy(first, second, keySelector))
}
// [UnionByEq] produces the set union of two sequences according to
// a specified key selector function and using a specified key equaler.
// If 'equaler' is nil, [collate.DeepEqualer] is used.
//
// [UnionByEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.unionby
func UnionByEq[Source, Key any](first, second Enumerable[Source],
keySelector func(Source) Key, equaler collate.Equaler[Key]) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return DistinctByEq(ConcatMust(first, second), keySelector, equaler)
}
// UnionByEqMust is like [UnionByEq] but panics in case of error.
func UnionByEqMust[Source, Key any](first, second Enumerable[Source],
keySelector func(Source) Key, equaler collate.Equaler[Key]) Enumerable[Source] {
return errorhelper.Must(UnionByEq(first, second, keySelector, equaler))
}
// [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 Enumerable[Source],
keySelector func(Source) Key, comparer collate.Comparer[Key]) (Enumerable[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if comparer == nil {
return nil, errorhelper.CallerError(ErrNilComparer)
}
return DistinctByCmp(ConcatMust(first, second), keySelector, comparer)
}
// UnionByCmpMust is like [UnionByCmp] but panics in case of error.
func UnionByCmpMust[Source, Key any](first, second Enumerable[Source],
keySelector func(Source) Key, comparer collate.Comparer[Key]) Enumerable[Source] {
return errorhelper.Must(UnionByCmp(first, second, keySelector, comparer))
}