1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 00:02:28 +08:00

feat: add Count and CountBy function

This commit is contained in:
dudaodong
2022-12-03 13:04:12 +08:00
parent a044da7d2f
commit 0a8058956f
2 changed files with 26 additions and 4 deletions

View File

@@ -215,8 +215,21 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
return result
}
// Count iterates over elements of slice, returns a count of all matched elements
func Count[T any](slice []T, predicate func(index int, item T) bool) int {
// Count returns the number of occurrences of the given item in the slice
func Count[T comparable](slice []T, item T) int {
count := 0
for _, v := range slice {
if item == v {
count++
}
}
return count
}
// CountBy iterates over elements of slice with predicate function, returns the number of all matched elements
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int {
count := 0
for i, v := range slice {