-
Notifications
You must be signed in to change notification settings - Fork 6
/
union.go
46 lines (41 loc) · 1.58 KB
/
union.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [Union] produces the set union of two sequences using [generichelper.DeepEqual] to compare values.
//
// [Union]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.union
func Union[Source any](first, second iter.Seq[Source]) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
return UnionEq(first, second, generichelper.DeepEqual[Source])
}
// [UnionEq] produces the set union of two sequences using 'equal' to compare values.
//
// [UnionEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.union
func UnionEq[Source any](first, second iter.Seq[Source], equal func(Source, Source) bool) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if equal == nil {
return nil, errorhelper.CallerError(ErrNilEqual)
}
concat, _ := Concat(first, second)
return DistinctEq(concat, equal)
}
// [UnionCmp] produces the set union of two sequences using 'comparer' to compare values. (See [DistinctCmp].)
//
// [UnionCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.union
func UnionCmp[Source any](first, second iter.Seq[Source], compare func(Source, Source) int) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if compare == nil {
return nil, errorhelper.CallerError(ErrNilCompare)
}
concat, _ := Concat(first, second)
return DistinctCmp(concat, compare)
}