diff --git a/docs/slice.md b/docs/slice.md index b566e26..d7c93f2 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -29,6 +29,7 @@ import ( - [Compact](#Compact) - [Concat](#Concat) - [Count](#Count) +- [CountBy](#CountBy) - [Difference](#Difference) - [DifferenceBy](#DifferenceBy) - [DifferenceWith](#DifferenceWith) @@ -45,8 +46,8 @@ import ( - [ForEach](#ForEach) - [GroupBy](#GroupBy) - [GroupWith](#GroupWith) -- [IntSlice](#IntSlice) -- [InterfaceSlice](#InterfaceSlice) +- [IntSlicedeprecated](#IntSlice) +- [InterfaceSlicedeprecated](#InterfaceSlice) - [Intersection](#Intersection) - [InsertAt](#InsertAt) - [IndexOf](#IndexOf) @@ -61,9 +62,9 @@ import ( - [Shuffle](#Shuffle) - [Sort](#Sort) - [SortBy](#SortBy) -- [SortByFieldDeprecated](#SortByField) +- [SortByFielddeprecated](#SortByField) - [Some](#Some) -- [StringSlice](#StringSlice) +- [StringSlicedeprecated](#StringSlice) - [SymmetricDifference](#SymmetricDifference) - [ToSlice](#ToSlice) - [ToSlicePointer](#ToSlicePointer) @@ -233,12 +234,38 @@ func main() { ### Count -
Count iterates over elements of slice, returns a count of all matched elements.
+Returns the number of occurrences of the given item in the slice.
Signature: ```go -func Count[T any](slice []T, predicate func(index int, t T) bool) int +func Count[T comparable](slice []T, item T) int +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 3, 4, 5} + + fmt.Println(slice.Count(nums, 1)) //1 + fmt.Println(slice.Count(nums, 3)) //2 +} +``` + +### CountBy + +Iterates over elements of slice with predicate function, returns the number of all matched elements.
+ +Signature: + +```go +func CountBy[T any](slice []T, predicate func(index int, item T) bool) int ``` Example: @@ -251,15 +278,16 @@ import ( func main() { nums := []int{1, 2, 3, 4, 5, 6} - evenFunc := func(i, num int) bool { + evenFunc := func(_, num int) bool { return (num % 2) == 0 } - res := slice.Count(nums, evenFunc) + res := slice.CountBy(nums, evenFunc) fmt.Println(res) //3 } ``` + ### DifferenceCreates an slice of whose element not included in the other given slice.
@@ -722,7 +750,7 @@ func main() { } ``` -### IntSlice +### IntSlice (Deprecated: use generic feature of go1.18+ for replacement)Convert interface slice to int slice.
@@ -747,7 +775,7 @@ func main() { } ``` -### InterfaceSlice +### InterfaceSlice (Deprecated: use generic feature of go1.18+ for replacement)Convert value to interface slice.
@@ -1260,7 +1288,7 @@ func main() { } ``` -### StringSlice +### StringSlice (Deprecated: use generic feature of go1.18+ for replacement)Convert interface slice to string slice.
diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 7836c42..a68d4d7 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -31,6 +31,7 @@ import ( - [Compact](#Compact) - [Concat](#Concat) - [Count](#Count) +- [CountBy](#CountBy) - [Difference](#Difference) - [DifferenceBy](#DifferenceBy) - [DifferenceWith](#DifferenceWith) @@ -47,8 +48,8 @@ import ( - [ForEach](#ForEach) - [GroupBy](#GroupBy) - [GroupWith](#GroupWith) -- [IntSlice](#IntSlice) -- [InterfaceSlice](#InterfaceSlice) +- [IntSlicedeprecated](#IntSlice) +- [InterfaceSlicedeprecated](#InterfaceSlice) - [Intersection](#Intersection) - [InsertAt](#InsertAt) - [IndexOf](#IndexOf) @@ -63,9 +64,9 @@ import ( - [Shuffle](#Shuffle) - [Sort](#Sort) - [SortBy](#SortBy) -- [SortByFieldDeprecated](#SortByField) +- [SortByFielddeprecated](#SortByField) - [Some](#Some) -- [StringSlice](#StringSlice) +- [StringSlicedeprecated](#StringSlice) - [SymmetricDifference](#SymmetricDifference) - [ToSlice](#ToSlice) - [ToSlicePointer](#ToSlicePointer) @@ -236,12 +237,38 @@ func main() { ### Count -遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数
+返回切片中指定元素的个数
函数签名: ```go -func Count[T any](slice []T, predicate func(index int, t T) bool) int +func Count[T comparable](slice []T, item T) int +``` + +例子: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + nums := []int{1, 2, 3, 3, 4, 5} + + fmt.Println(slice.Count(nums, 1)) //1 + fmt.Println(slice.Count(nums, 3)) //2 +} +``` + +### CountBy + +遍历切片,对每个元素执行函数predicate. 返回符合函数返回值为true的元素的个数.
+ +函数签名: + +```go +func CountBy[T any](slice []T, predicate func(index int, item T) bool) int ``` 例子: @@ -254,11 +281,11 @@ import ( func main() { nums := []int{1, 2, 3, 4, 5, 6} - evenFunc := func(i, num int) bool { + evenFunc := func(_, num int) bool { return (num % 2) == 0 } - res := slice.Count(nums, evenFunc) + res := slice.CountBy(nums, evenFunc) fmt.Println(res) //3 } ``` @@ -725,7 +752,7 @@ func main() { } ``` -### IntSlice +### IntSlice (已弃用: 使用go1.18+泛型代替)将接口切片转换为int切片
@@ -750,7 +777,7 @@ func main() { } ``` -### InterfaceSlice +### InterfaceSlice(已弃用: 使用go1.18+泛型代替)将值转换为接口切片
@@ -1259,7 +1286,7 @@ func main() { } ``` -### StringSlice +### StringSlice(已弃用: 使用go1.18+泛型代替)将接口切片转换为字符串切片