diff --git a/function/watcher_test.go b/function/watcher_test.go index fb7c981..788f055 100644 --- a/function/watcher_test.go +++ b/function/watcher_test.go @@ -1,7 +1,6 @@ package function import ( - "fmt" "testing" "github.com/duke-git/lancet/v2/internal" @@ -30,10 +29,10 @@ func TestWatcher(t *testing.T) { assert.Equal(int64(0), w.stopTime) } -func longRunningTask() { - var slice []int64 +func longRunningTask() []int64 { + var data []int64 for i := 0; i < 10000000; i++ { - slice = append(slice, int64(i)) + data = append(data, int64(i)) } - fmt.Println(slice) + return data } diff --git a/slice/slice.go b/slice/slice.go index 3d19a6e..e843812 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -69,7 +69,7 @@ func Chunk[T any](slice []T, size int) [][]T { // Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey func Compact[T any](slice []T) []T { - result := make([]T, 0, 0) + result := make([]T, 0) for _, v := range slice { if !reflect.DeepEqual(v, nil) && !reflect.DeepEqual(v, false) && @@ -112,7 +112,7 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde orginSliceAfterMap := Map(slice, iteratee) comparedSliceAfterMap := Map(comparedSlice, iteratee) - result := make([]T, 0, 0) + result := make([]T, 0) for i, v := range orginSliceAfterMap { if !Contain(comparedSliceAfterMap, v) { result = append(result, slice[i]) @@ -124,7 +124,7 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde //DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal). func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T { - result := make([]T, 0, 0) + result := make([]T, 0) getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int { index := -1 @@ -230,7 +230,7 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T { panic("predicate func is missing") } - result := make([]T, 0, 0) + result := make([]T, 0) for i, v := range slice { if predicate(i, v) { result = append(result, v) @@ -443,7 +443,7 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia result := iteratee(0, initial, slice[0]) - tmp := make([]T, 2, 2) + tmp := make([]T, 2) for i := 1; i < len(slice); i++ { tmp[0] = result tmp[1] = slice[i] @@ -542,7 +542,7 @@ func DeleteAt[T any](slice []T, start int, end ...int) []T { } if start == size-1 { - slice = append(slice[:start]) + slice = slice[:start] } else { slice = append(slice[:start], slice[start+1:]...) } @@ -652,9 +652,7 @@ func Union[T comparable](slices ...[]T) []T { var allElements []T for _, slice := range slices { - for _, v := range slice { - allElements = append(allElements, v) - } + allElements = append(allElements, slice...) } return Unique(allElements) @@ -689,7 +687,7 @@ func Intersection[T comparable](slices ...[]T) []T { result = reducer(slices[0], slices[1]) - reduceSlice := make([][]T, 2, 2) + reduceSlice := make([][]T, 2) for i := 2; i < len(slices); i++ { reduceSlice[0] = result reduceSlice[1] = slices[i] @@ -866,9 +864,7 @@ func ToSlicePointer[T any](value ...T) []*T { // ToSlice returns a slices of a variable parameter transformation func ToSlice[T any](value ...T) []T { out := make([]T, len(value)) - for i := range value { - out[i] = value[i] - } + copy(out, value) return out } diff --git a/slice/slice_internal.go b/slice/slice_internal.go index 0c9907d..30f43db 100644 --- a/slice/slice_internal.go +++ b/slice/slice_internal.go @@ -14,45 +14,6 @@ func sliceValue(slice any) reflect.Value { return v } -// functionValue return the reflect value of a function -func functionValue(function any) reflect.Value { - v := reflect.ValueOf(function) - if v.Kind() != reflect.Func { - panic(fmt.Sprintf("Invalid function type, value of type %T", function)) - } - return v -} - -// checkSliceCallbackFuncSignature Check func sign : s :[]type1{} -> func(i int, data type1) type2 -// see https://coolshell.cn/articles/21164.html#%E6%B3%9B%E5%9E%8BMap-Reduce -func checkSliceCallbackFuncSignature(fn reflect.Value, types ...reflect.Type) bool { - //Check it is a function - if fn.Kind() != reflect.Func { - return false - } - // NumIn() - returns a function type's input parameter count. - // NumOut() - returns a function type's output parameter count. - if (fn.Type().NumIn() != len(types)-1) || (fn.Type().NumOut() != 1) { - return false - } - // In() - returns the type of a function type's i'th input parameter. - // first input param type should be int - if fn.Type().In(0) != reflect.TypeOf(1) { - return false - } - for i := 0; i < len(types)-1; i++ { - if fn.Type().In(i) != types[i] { - return false - } - } - // Out() - returns the type of a function type's i'th output parameter. - outType := types[len(types)-1] - if outType != nil && fn.Type().Out(0) != outType { - return false - } - return true -} - // sliceElemType get slice element type func sliceElemType(reflectType reflect.Type) reflect.Type { for {