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

Compare commits

...

2 Commits

Author SHA1 Message Date
dudaodong
96a4327aa7 fix: fix some static check issues 2022-10-15 15:03:24 +08:00
dudaodong
fcfbdea597 feat: add Replace and ReplaceAll for slice 2022-10-15 14:49:49 +08:00
3 changed files with 33 additions and 57 deletions

View File

@@ -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
}

View File

@@ -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]
@@ -453,6 +453,26 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
return result
}
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new
func Replace[T comparable](slice []T, old T, new T, n int) []T {
result := make([]T, len(slice))
copy(result, slice)
for i := range result {
if result[i] == old && n != 0 {
result[i] = new
n--
}
}
return result
}
// ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new.
func ReplaceAll[T comparable](slice []T, old T, new T) []T {
return Replace(slice, old, new, -1)
}
// InterfaceSlice convert param to slice of interface.
func InterfaceSlice(slice any) []any {
sv := sliceValue(slice)
@@ -522,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:]...)
}
@@ -632,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)
@@ -669,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]
@@ -846,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
}

View File

@@ -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 {