mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
fix: fix some static check issues
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package function
|
package function
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -30,10 +29,10 @@ func TestWatcher(t *testing.T) {
|
|||||||
assert.Equal(int64(0), w.stopTime)
|
assert.Equal(int64(0), w.stopTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func longRunningTask() {
|
func longRunningTask() []int64 {
|
||||||
var slice []int64
|
var data []int64
|
||||||
for i := 0; i < 10000000; i++ {
|
for i := 0; i < 10000000; i++ {
|
||||||
slice = append(slice, int64(i))
|
data = append(data, int64(i))
|
||||||
}
|
}
|
||||||
fmt.Println(slice)
|
return data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
|
||||||
func Compact[T any](slice []T) []T {
|
func Compact[T any](slice []T) []T {
|
||||||
result := make([]T, 0, 0)
|
result := make([]T, 0)
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if !reflect.DeepEqual(v, nil) &&
|
if !reflect.DeepEqual(v, nil) &&
|
||||||
!reflect.DeepEqual(v, false) &&
|
!reflect.DeepEqual(v, false) &&
|
||||||
@@ -112,7 +112,7 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
|
|||||||
orginSliceAfterMap := Map(slice, iteratee)
|
orginSliceAfterMap := Map(slice, iteratee)
|
||||||
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
comparedSliceAfterMap := Map(comparedSlice, iteratee)
|
||||||
|
|
||||||
result := make([]T, 0, 0)
|
result := make([]T, 0)
|
||||||
for i, v := range orginSliceAfterMap {
|
for i, v := range orginSliceAfterMap {
|
||||||
if !Contain(comparedSliceAfterMap, v) {
|
if !Contain(comparedSliceAfterMap, v) {
|
||||||
result = append(result, slice[i])
|
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).
|
//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 {
|
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 {
|
getIndex := func(arr []T, item T, comparison func(v1, v2 T) bool) int {
|
||||||
index := -1
|
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")
|
panic("predicate func is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]T, 0, 0)
|
result := make([]T, 0)
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
result = append(result, 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])
|
result := iteratee(0, initial, slice[0])
|
||||||
|
|
||||||
tmp := make([]T, 2, 2)
|
tmp := make([]T, 2)
|
||||||
for i := 1; i < len(slice); i++ {
|
for i := 1; i < len(slice); i++ {
|
||||||
tmp[0] = result
|
tmp[0] = result
|
||||||
tmp[1] = slice[i]
|
tmp[1] = slice[i]
|
||||||
@@ -542,7 +542,7 @@ func DeleteAt[T any](slice []T, start int, end ...int) []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if start == size-1 {
|
if start == size-1 {
|
||||||
slice = append(slice[:start])
|
slice = slice[:start]
|
||||||
} else {
|
} else {
|
||||||
slice = append(slice[:start], slice[start+1:]...)
|
slice = append(slice[:start], slice[start+1:]...)
|
||||||
}
|
}
|
||||||
@@ -652,9 +652,7 @@ func Union[T comparable](slices ...[]T) []T {
|
|||||||
var allElements []T
|
var allElements []T
|
||||||
|
|
||||||
for _, slice := range slices {
|
for _, slice := range slices {
|
||||||
for _, v := range slice {
|
allElements = append(allElements, slice...)
|
||||||
allElements = append(allElements, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(allElements)
|
return Unique(allElements)
|
||||||
@@ -689,7 +687,7 @@ func Intersection[T comparable](slices ...[]T) []T {
|
|||||||
|
|
||||||
result = reducer(slices[0], slices[1])
|
result = reducer(slices[0], slices[1])
|
||||||
|
|
||||||
reduceSlice := make([][]T, 2, 2)
|
reduceSlice := make([][]T, 2)
|
||||||
for i := 2; i < len(slices); i++ {
|
for i := 2; i < len(slices); i++ {
|
||||||
reduceSlice[0] = result
|
reduceSlice[0] = result
|
||||||
reduceSlice[1] = slices[i]
|
reduceSlice[1] = slices[i]
|
||||||
@@ -866,9 +864,7 @@ func ToSlicePointer[T any](value ...T) []*T {
|
|||||||
// ToSlice returns a slices of a variable parameter transformation
|
// ToSlice returns a slices of a variable parameter transformation
|
||||||
func ToSlice[T any](value ...T) []T {
|
func ToSlice[T any](value ...T) []T {
|
||||||
out := make([]T, len(value))
|
out := make([]T, len(value))
|
||||||
for i := range value {
|
copy(out, value)
|
||||||
out[i] = value[i]
|
|
||||||
}
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,45 +14,6 @@ func sliceValue(slice any) reflect.Value {
|
|||||||
return v
|
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
|
// sliceElemType get slice element type
|
||||||
func sliceElemType(reflectType reflect.Type) reflect.Type {
|
func sliceElemType(reflectType reflect.Type) reflect.Type {
|
||||||
for {
|
for {
|
||||||
|
|||||||
Reference in New Issue
Block a user