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

Slice: group by and without v2 (#20)

* Slice: group by and without v2

Migrate from reflection to generic

* update doc
This commit is contained in:
donutloop
2022-01-11 11:11:01 +01:00
committed by GitHub
parent 24c0d95112
commit c906d8aea7
5 changed files with 27 additions and 34 deletions

View File

@@ -418,8 +418,8 @@ func StringSlice(slice interface{}) []string //convert value to string slice
func Unique(slice interface{}) interface{} //remove duplicate elements in slice func Unique(slice interface{}) interface{} //remove duplicate elements in slice
func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
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[T comparable](slice []T, values ...T) []T//creates a slice excluding all given values
func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) // groups slice into two categories
func 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 func 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
``` ```

View File

@@ -419,8 +419,8 @@ func StringSlice(slice interface{}) []string //转为string切片
func Unique(slice interface{}) interface{} //去重切片 func Unique(slice interface{}) interface{} //去重切片
func Union(slices ...interface{}) interface{} //slice并集, 去重 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[T comparable](slice []T, values ...T) []T//slice去除values
func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) //根据函数function的逻辑分slice为两组slice
func Count[T any](slice []T, fn func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的计数 func Count[T any](slice []T, fn func(index int, t T) bool) int //遍历slice的元素返回所有匹配元素的计数
``` ```

View File

@@ -1,6 +1,5 @@
package myconstraints package myconstraints
type Number interface { type Number interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | complex64 | complex128 int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | complex64 | complex128
} }

View File

@@ -144,28 +144,29 @@ func Count[T any](slice []T, fn func(index int, t T) bool) int {
// 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
// The function signature should be func(index int, value interface{}) bool . // The function signature should be func(index int, value interface{}) bool .
func GroupBy(slice, function interface{}) (interface{}, interface{}) { func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
sv := sliceValue(slice)
fn := functionValue(function)
elemType := sv.Type().Elem() if fn == nil {
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) { panic("fn is missing")
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
} }
groupB := reflect.MakeSlice(sv.Type(), 0, 0) if len(slice) == 0 {
groupA := reflect.MakeSlice(sv.Type(), 0, 0) return make([]T, 0), make([]T, 0)
}
for i := 0; i < sv.Len(); i++ { groupB := make([]T, 0)
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0] groupA := make([]T, 0)
if flag.Bool() {
groupA = reflect.Append(groupA, sv.Index(i)) for i, v := range slice {
ok := fn(i, v)
if ok {
groupA = append(groupA, v)
} else { } else {
groupB = reflect.Append(groupB, sv.Index(i)) groupB = append(groupB, v)
} }
} }
return groupA.Interface(), groupB.Interface() return groupA, groupB
} }
// Find iterates over elements of slice, returning the first one that passes a truth test on function. // Find iterates over elements of slice, returning the first one that passes a truth test on function.
@@ -587,24 +588,17 @@ func reverseSlice(slice interface{}) {
} }
// Without creates a slice excluding all given values // Without creates a slice excluding all given values
func Without(slice interface{}, values ...interface{}) interface{} { func Without[T comparable](slice []T, values ...T) []T {
sv := sliceValue(slice) if len(values) == 0 || len(slice) == 0 {
if sv.Len() == 0 {
return slice return slice
} }
var indexes []int out := make([]T, 0, len(slice))
for i := 0; i < sv.Len(); i++ { for _, v := range slice {
v := sv.Index(i).Interface()
if !Contain(values, v) { if !Contain(values, v) {
indexes = append(indexes, i) out = append(out, v)
} }
} }
res := reflect.MakeSlice(sv.Type(), len(indexes), len(indexes)) return out
for i := range indexes {
res.Index(i).Set(sv.Index(indexes[i]))
}
return res.Interface()
} }