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

Slice: CountV2 (#18)

Use generic instead of reflection
This commit is contained in:
donutloop
2022-01-09 15:15:03 +01:00
committed by GitHub
parent c4b4cb1173
commit 7b9a8a55e7
3 changed files with 14 additions and 15 deletions

View File

@@ -420,7 +420,7 @@ func Union(slices ...interface{}) interface{} //Union creates a slice of unique
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index. func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values
func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories
func Count(slice, function interface{}) int // Count iterates over elements of slice, returns a count of all matched elements Count[T any](slice []T, fn func(index int, t T) bool) int // Count iterates over elements of slice, returns a count of all matched elements
``` ```
#### 10. strutil is for processing string #### 10. strutil is for processing string

View File

@@ -421,7 +421,7 @@ func Union(slices ...interface{}) interface{} //slice并集, 去重
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice
func Count(slice, function interface{}) int Count[T any](slice []T, fn func(index int, t T) bool) int
``` ```
#### 10. strutil字符串处理包 #### 10. strutil字符串处理包

View File

@@ -179,24 +179,23 @@ func Filter[T any](slice []T, fn func(index int, t T) bool) []T {
// Count iterates over elements of slice, returns a count of all matched elements // Count iterates over elements of slice, returns a count of all matched elements
// The function signature should be func(index int, value interface{}) bool . // The function signature should be func(index int, value interface{}) bool .
func Count(slice, function interface{}) int { func Count[T any](slice []T, fn func(index int, t T) bool) int {
sv := sliceValue(slice) if fn == nil {
fn := functionValue(function) panic("fn is missing")
elemType := sv.Type().Elem()
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
} }
var counter int if len(slice) == 0 {
for i := 0; i < sv.Len(); i++ { return 0
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0] }
if flag.Bool() {
counter++ var count int
for i, v := range slice {
if fn(i, v) {
count++
} }
} }
return counter return count
} }
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices // GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices