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

doc: add doc for Count and CountBy function

This commit is contained in:
dudaodong
2022-12-03 14:20:37 +08:00
parent 18914ee2cd
commit 61c43daabb
2 changed files with 77 additions and 22 deletions

View File

@@ -29,6 +29,7 @@ import (
- [Compact](#Compact) - [Compact](#Compact)
- [Concat](#Concat) - [Concat](#Concat)
- [Count](#Count) - [Count](#Count)
- [CountBy](#CountBy)
- [Difference](#Difference) - [Difference](#Difference)
- [DifferenceBy](#DifferenceBy) - [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith) - [DifferenceWith](#DifferenceWith)
@@ -45,8 +46,8 @@ import (
- [ForEach](#ForEach) - [ForEach](#ForEach)
- [GroupBy](#GroupBy) - [GroupBy](#GroupBy)
- [GroupWith](#GroupWith) - [GroupWith](#GroupWith)
- [IntSlice](#IntSlice) - [IntSlice<sup>deprecated</sup>](#IntSlice)
- [InterfaceSlice](#InterfaceSlice) - [InterfaceSlice<sup>deprecated</sup>](#InterfaceSlice)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [InsertAt](#InsertAt) - [InsertAt](#InsertAt)
- [IndexOf](#IndexOf) - [IndexOf](#IndexOf)
@@ -61,9 +62,9 @@ import (
- [Shuffle](#Shuffle) - [Shuffle](#Shuffle)
- [Sort](#Sort) - [Sort](#Sort)
- [SortBy](#SortBy) - [SortBy](#SortBy)
- [SortByField<sup>Deprecated</sup>](#SortByField) - [SortByField<sup>deprecated</sup>](#SortByField)
- [Some](#Some) - [Some](#Some)
- [StringSlice](#StringSlice) - [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference) - [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice) - [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer) - [ToSlicePointer](#ToSlicePointer)
@@ -233,12 +234,38 @@ func main() {
### <span id="Count">Count</span> ### <span id="Count">Count</span>
<p>Count iterates over elements of slice, returns a count of all matched elements.</p> <p>Returns the number of occurrences of the given item in the slice.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func Count[T any](slice []T, predicate func(index int, t T) bool) int func Count[T comparable](slice []T, item T) int
```
<b>Example:</b>
```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
}
```
### <span id="CountBy">CountBy</span>
<p>Iterates over elements of slice with predicate function, returns the number of all matched elements.</p>
<b>Signature:</b>
```go
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
``` ```
<b>Example:</b> <b>Example:</b>
@@ -251,15 +278,16 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool { evenFunc := func(_, num int) bool {
return (num % 2) == 0 return (num % 2) == 0
} }
res := slice.Count(nums, evenFunc) res := slice.CountBy(nums, evenFunc)
fmt.Println(res) //3 fmt.Println(res) //3
} }
``` ```
### <span id="Difference">Difference</span> ### <span id="Difference">Difference</span>
<p>Creates an slice of whose element not included in the other given slice.</p> <p>Creates an slice of whose element not included in the other given slice.</p>
@@ -722,7 +750,7 @@ func main() {
} }
``` ```
### <span id="IntSlice">IntSlice</span> ### <span id="IntSlice">IntSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert interface slice to int slice.</p> <p>Convert interface slice to int slice.</p>
@@ -747,7 +775,7 @@ func main() {
} }
``` ```
### <span id="InterfaceSlice">InterfaceSlice</span> ### <span id="InterfaceSlice">InterfaceSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert value to interface slice.</p> <p>Convert value to interface slice.</p>
@@ -1260,7 +1288,7 @@ func main() {
} }
``` ```
### <span id="StringSlice">StringSlice</span> ### <span id="StringSlice">StringSlice (Deprecated: use generic feature of go1.18+ for replacement)</span>
<p>Convert interface slice to string slice.</p> <p>Convert interface slice to string slice.</p>

View File

@@ -31,6 +31,7 @@ import (
- [Compact](#Compact) - [Compact](#Compact)
- [Concat](#Concat) - [Concat](#Concat)
- [Count](#Count) - [Count](#Count)
- [CountBy](#CountBy)
- [Difference](#Difference) - [Difference](#Difference)
- [DifferenceBy](#DifferenceBy) - [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith) - [DifferenceWith](#DifferenceWith)
@@ -47,8 +48,8 @@ import (
- [ForEach](#ForEach) - [ForEach](#ForEach)
- [GroupBy](#GroupBy) - [GroupBy](#GroupBy)
- [GroupWith](#GroupWith) - [GroupWith](#GroupWith)
- [IntSlice](#IntSlice) - [IntSlice<sup>deprecated</sup>](#IntSlice)
- [InterfaceSlice](#InterfaceSlice) - [InterfaceSlice<sup>deprecated</sup>](#InterfaceSlice)
- [Intersection](#Intersection) - [Intersection](#Intersection)
- [InsertAt](#InsertAt) - [InsertAt](#InsertAt)
- [IndexOf](#IndexOf) - [IndexOf](#IndexOf)
@@ -63,9 +64,9 @@ import (
- [Shuffle](#Shuffle) - [Shuffle](#Shuffle)
- [Sort](#Sort) - [Sort](#Sort)
- [SortBy](#SortBy) - [SortBy](#SortBy)
- [SortByField<sup>Deprecated</sup>](#SortByField) - [SortByField<sup>deprecated</sup>](#SortByField)
- [Some](#Some) - [Some](#Some)
- [StringSlice](#StringSlice) - [StringSlice<sup>deprecated</sup>](#StringSlice)
- [SymmetricDifference](#SymmetricDifference) - [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice) - [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer) - [ToSlicePointer](#ToSlicePointer)
@@ -236,12 +237,38 @@ func main() {
### <span id="Count">Count</span> ### <span id="Count">Count</span>
<p>遍历切片对每个元素执行函数function. 返回符合函数返回值为true的元素的个数</p> <p>返回切片中指定元素的个数</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Count[T any](slice []T, predicate func(index int, t T) bool) int func Count[T comparable](slice []T, item T) int
```
<b>例子:</b>
```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
}
```
### <span id="CountBy">CountBy</span>
<p>遍历切片对每个元素执行函数predicate. 返回符合函数返回值为true的元素的个数.</p>
<b>函数签名:</b>
```go
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
``` ```
<b>例子:</b> <b>例子:</b>
@@ -254,11 +281,11 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
evenFunc := func(i, num int) bool { evenFunc := func(_, num int) bool {
return (num % 2) == 0 return (num % 2) == 0
} }
res := slice.Count(nums, evenFunc) res := slice.CountBy(nums, evenFunc)
fmt.Println(res) //3 fmt.Println(res) //3
} }
``` ```
@@ -725,7 +752,7 @@ func main() {
} }
``` ```
### <span id="IntSlice">IntSlice</span> ### <span id="IntSlice">IntSlice (已弃用: 使用go1.18+泛型代替)</span>
<p>将接口切片转换为int切片</p> <p>将接口切片转换为int切片</p>
@@ -750,7 +777,7 @@ func main() {
} }
``` ```
### <span id="InterfaceSlice">InterfaceSlice</span> ### <span id="InterfaceSlice">InterfaceSlice(已弃用: 使用go1.18+泛型代替)</span>
<p>将值转换为接口切片</p> <p>将值转换为接口切片</p>
@@ -1259,7 +1286,7 @@ func main() {
} }
``` ```
### <span id="StringSlice">StringSlice</span> ### <span id="StringSlice">StringSlice(已弃用: 使用go1.18+泛型代替)</span>
<p>将接口切片转换为字符串切片</p> <p>将接口切片转换为字符串切片</p>