-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontains.go
40 lines (36 loc) · 1.14 KB
/
contains.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [Contains] determines whether a sequence contains a specified element using [generichelper.DeepEqual].
//
// [Contains]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.contains
func Contains[Source any](source iter.Seq[Source], value Source) (bool, error) {
if source == nil {
return false, errorhelper.CallerError(ErrNilSource)
}
r, err := ContainsEq(source, value, generichelper.DeepEqual[Source])
if err != nil {
return false, errorhelper.CallerError(err)
}
return r, nil
}
// [ContainsEq] determines whether a sequence contains a specified element using a specified 'equal'.
//
// [ContainsEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.contains
func ContainsEq[Source any](source iter.Seq[Source], value Source, equal func(Source, Source) bool) (bool, error) {
if source == nil {
return false, errorhelper.CallerError(ErrNilSource)
}
if equal == nil {
return false, errorhelper.CallerError(ErrNilEqual)
}
for s := range source {
if equal(s, value) {
return true, nil
}
}
return false, nil
}