-
Notifications
You must be signed in to change notification settings - Fork 3
/
count.go
48 lines (43 loc) · 957 Bytes
/
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
package sliceKit
import "github.com/samber/lo"
// Count
/*
e.g.
count := lo.Count([]int{1, 5, 1}, 1)
// 2
*/
func Count[T comparable](collection []T, value T) (count int) {
return lo.Count(collection, value)
}
// CountBy
/*
e.g.
count := lo.CountBy([]int{1, 5, 1}, func(i int) bool {
return i < 4
})
// 2
*/
func CountBy[T any](collection []T, predicate func(item T) bool) (count int) {
return lo.CountBy(collection, predicate)
}
// CountValues 计算集合中每个元素的个数.
/*
e.g.
lo.CountValues([]int{1, 2, 2})
// map[int]int{1: 1, 2: 2}
*/
func CountValues[T comparable](collection []T) map[T]int {
return lo.CountValues(collection)
}
// CountValuesBy
/*
e.g.
isEven := func(v int) bool {
return v%2==0
}
lo.CountValuesBy([]int{1, 2, 2}, isEven)
// map[bool]int{false: 1, true: 2}
*/
func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) map[U]int {
return lo.CountValuesBy(collection, mapper)
}