-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontains.go
41 lines (37 loc) · 799 Bytes
/
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
41
package linq
// Contains cetermines whether a sequence contains a specified element.
func Contains[T comparable, E IEnumerable[T]](src E, val T) (bool, error) {
e := src()
for {
t, err := e.Next()
if err != nil {
if isEOC(err) {
return false, nil
}
return false, err
}
if t == val {
return true, nil
}
}
}
// ContainsFunc determines whether a sequence contains a specified element by using a specified comparer function.
func ContainsFunc[T any, E IEnumerable[T]](src E, val T, equals func(T, T) (bool, error)) (bool, error) {
e := src()
for {
t, err := e.Next()
if err != nil {
if isEOC(err) {
return false, nil
}
return false, err
}
eq, err := equals(t, val)
if err != nil {
return false, err
}
if eq {
return true, nil
}
}
}