-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathany.go
38 lines (34 loc) · 926 Bytes
/
any.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
)
// [Any] determines whether a sequence contains any elements.
//
// [Any]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.any
func Any[Source any](source iter.Seq[Source]) (bool, error) {
if source == nil {
return false, errorhelper.CallerError(ErrNilSource)
}
for range source {
return true, nil
}
return false, nil
}
// [AnyPred] determines whether any element of a sequence satisfies a condition.
//
// [AnyPred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.any
func AnyPred[Source any](source iter.Seq[Source], predicate func(Source) bool) (bool, error) {
if source == nil {
return false, errorhelper.CallerError(ErrNilSource)
}
if predicate == nil {
return false, errorhelper.CallerError(ErrNilPredicate)
}
for s := range source {
if predicate(s) {
return true, nil
}
}
return false, nil
}