/
all.go
33 lines (29 loc) · 973 Bytes
/
all.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
package go2linq
import (
"github.com/solsw/generichelper"
)
// Reimplementing LINQ to Objects: Part 10 - Any and All
// https://codeblog.jonskeet.uk/2010/12/28/reimplementing-linq-to-objects-part-10-any-and-all/
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.all
// [All] determines whether all elements of a sequence satisfy a condition.
//
// [All]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.all
func All[Source any](source Enumerable[Source], predicate func(Source) bool) (bool, error) {
if source == nil {
return false, ErrNilSource
}
if predicate == nil {
return false, ErrNilPredicate
}
enr := source.GetEnumerator()
for enr.MoveNext() {
if !predicate(enr.Current()) {
return false, nil
}
}
return true, nil
}
// AllMust is like [All] but panics in case of error.
func AllMust[Source any](source Enumerable[Source], predicate func(Source) bool) bool {
return generichelper.Must(All(source, predicate))
}