-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexcept.go
62 lines (57 loc) · 2.26 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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [Except] produces the set difference of two sequences using [generichelper.DeepEqual] to compare values.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [Except]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.except
func Except[Source any](first, second iter.Seq[Source]) (iter.Seq[Source], error) {
if first == nil || second == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
r, err := ExceptEq(first, second, generichelper.DeepEqual[Source])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ExceptEq] produces the set difference of two sequences using 'equal' to compare values.
// 'second' is enumerated on the first iteration over the result.
// Order of elements in the result corresponds to the order of elements in 'first'.
//
// [ExceptEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.except
func ExceptEq[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)
}
r, err := ExceptByEq(first, second, Identity[Source], equal)
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ExceptCmp] produces the set difference of two sequences using 'compare' to compare values. (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'.
//
// [ExceptCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.except
func ExceptCmp[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)
}
r, err := ExceptByCmp(first, second, Identity[Source], compare)
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}