-
Notifications
You must be signed in to change notification settings - Fork 6
/
count.go
59 lines (53 loc) · 1.73 KB
/
count.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
package go2linq
import (
"github.com/solsw/errorhelper"
)
// Reimplementing LINQ to Objects: Part 7 - Count and LongCount
// https://codeblog.jonskeet.uk/2010/12/26/reimplementing-linq-to-objects-part-7-count-and-longcount/
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.count
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.longcount
// [Count] returns the number of elements in a sequence.
//
// [Count]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.count
func Count[Source any](source Enumerable[Source]) (int, error) {
if source == nil {
return -1, errorhelper.CallerError(ErrNilSource)
}
var c int
if TryGetNonEnumeratedCountMust(source, &c) {
return c, nil
}
enr := source.GetEnumerator()
r := 0
for enr.MoveNext() {
r++
}
return r, nil
}
// CountMust is like [Count] but panics in case of error.
func CountMust[Source any](source Enumerable[Source]) int {
return errorhelper.Must(Count(source))
}
// [CountPred] returns a number that represents how many elements in a specified sequence satisfy a condition.
//
// [CountPred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.count
func CountPred[Source any](source Enumerable[Source], predicate func(Source) bool) (int, error) {
if source == nil {
return -1, errorhelper.CallerError(ErrNilSource)
}
if predicate == nil {
return -1, errorhelper.CallerError(ErrNilPredicate)
}
enr := source.GetEnumerator()
r := 0
for enr.MoveNext() {
if predicate(enr.Current()) {
r++
}
}
return r, nil
}
// CountPredMust is like [CountPred] but panics in case of error.
func CountPredMust[Source any](source Enumerable[Source], predicate func(Source) bool) int {
return errorhelper.Must(CountPred(source, predicate))
}