mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 22:22:29 +08:00
merge #pr20
This commit is contained in:
@@ -415,11 +415,19 @@ func Shuffle[T any](slice []T) []T //creates an slice of shuffled values
|
||||
func SortByField(slice interface{}, field string, sortType ...string) error //sort struct slice by field
|
||||
func Some[T any](slice []T, fn func(index int, t T) bool) bool //return true if any of the values in the list pass the predicate fn function
|
||||
func StringSlice(slice interface{}) []string //convert value to string slice
|
||||
<<<<<<< HEAD
|
||||
func Unique[T comparable](slice []T) []T //remove duplicate elements in slice
|
||||
func Union[T comparable](slices ...[]T) []T //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons.
|
||||
func UpdateByIndex[T any](slice []T, index int, value T) []T //update the slice element at index.
|
||||
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 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 UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index.
|
||||
func Without[T comparable](slice []T, values ...T) []T//creates a slice excluding all given values
|
||||
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) // groups slice into two categories
|
||||
>>>>>>> c906d8aea7d772f6f32821e6563aa398dde22127
|
||||
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
|
||||
```
|
||||
|
||||
|
||||
@@ -416,11 +416,19 @@ func Shuffle[T any](slice []T) []T //创建一个被打乱值的切片
|
||||
func Some[T any](slice []T, fn func(index int, t T) bool) bool //slice中任意一个元素都符合函数条件时返回true, 否则返回false.
|
||||
func SortByField(slice interface{}, field string, sortType ...string) error //对struct切片进行排序
|
||||
func StringSlice(slice interface{}) []string //转为string切片
|
||||
<<<<<<< HEAD
|
||||
func Unique[T comparable](slice []T) []T //去重切片
|
||||
func Union[T comparable](slices ...[]T) []T //slice并集, 去重
|
||||
func UpdateByIndex[T any](slice []T, index int, value T) []T //在切片中index位置更新value
|
||||
func Without[T comparable](slice []T, values ...T) []T //slice去除values
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice
|
||||
=======
|
||||
func Unique(slice interface{}) interface{} //去重切片
|
||||
func Union(slices ...interface{}) interface{} //slice并集, 去重
|
||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value
|
||||
func Without[T comparable](slice []T, values ...T) []T//slice去除values
|
||||
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) //根据函数function的逻辑分slice为两组slice
|
||||
>>>>>>> c906d8aea7d772f6f32821e6563aa398dde22127
|
||||
func Count[T any](slice []T, fn func(index int, t T) bool) int //遍历slice的元素,返回所有匹配元素的计数
|
||||
```
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestWatcher(t *testing.T) {
|
||||
assert.Equal(false, w.excuting)
|
||||
|
||||
w.Reset()
|
||||
|
||||
|
||||
assert.Equal(int64(0), w.startTime)
|
||||
assert.Equal(int64(0), w.stopTime)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package myconstraints
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -143,28 +143,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
|
||||
// The function signature should be func(index int, value interface{}) bool .
|
||||
func GroupBy(slice, function interface{}) (interface{}, interface{}) {
|
||||
sv := sliceValue(slice)
|
||||
fn := functionValue(function)
|
||||
func GroupBy[T any](slice []T, fn func(index int, t T) bool) ([]T, []T) {
|
||||
|
||||
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())
|
||||
if fn == nil {
|
||||
panic("fn is missing")
|
||||
}
|
||||
|
||||
groupB := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||
groupA := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||
if len(slice) == 0 {
|
||||
return make([]T, 0), make([]T, 0)
|
||||
}
|
||||
|
||||
for i := 0; i < sv.Len(); i++ {
|
||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||
if flag.Bool() {
|
||||
groupA = reflect.Append(groupA, sv.Index(i))
|
||||
groupB := make([]T, 0)
|
||||
groupA := make([]T, 0)
|
||||
|
||||
for i, v := range slice {
|
||||
ok := fn(i, v)
|
||||
if ok {
|
||||
groupA = append(groupA, v)
|
||||
} 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.
|
||||
@@ -566,18 +567,17 @@ func reverseSlice(slice interface{}) {
|
||||
}
|
||||
|
||||
// Without creates a slice excluding all given values
|
||||
// func Without[T comparable](slice []T, values ...T) []T {
|
||||
// var indexes []int
|
||||
// for i := 0; i < len(slice); i++ {
|
||||
// if !Contain(values, slice[i]) {
|
||||
// indexes = append(indexes, i)
|
||||
// }
|
||||
// }
|
||||
func Without[T comparable](slice []T, values ...T) []T {
|
||||
if len(values) == 0 || len(slice) == 0 {
|
||||
return slice
|
||||
}
|
||||
|
||||
// res := make([]T, len(indexes), len(indexes))
|
||||
// for i, v := range indexes {
|
||||
// res[i] = slice[v]
|
||||
// }
|
||||
out := make([]T, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
if !Contain(values, v) {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
|
||||
// return res
|
||||
// }
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user