mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
checkout v2 branch for generics migration
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module github.com/duke-git/lancet
|
module github.com/duke-git/lancet
|
||||||
|
|
||||||
go 1.16
|
go 1.18
|
||||||
|
|||||||
6
myconstraints/constraints.go
Normal file
6
myconstraints/constraints.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package myconstraints
|
||||||
|
|
||||||
|
|
||||||
|
type Number interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | complex64 | complex128
|
||||||
|
}
|
||||||
@@ -166,25 +166,15 @@ func Some(slice, function interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for.
|
// Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for.
|
||||||
// The function signature should be func(index int, value interface{}) bool .
|
// The fn signature should be func(int, T) bool.
|
||||||
func Filter(slice, function interface{}) interface{} {
|
func Filter[T any](slice []T, fn func(index int, t T) bool) []T {
|
||||||
sv := sliceValue(slice)
|
res := make([]T, 0, 0)
|
||||||
fn := functionValue(function)
|
for i, v := range slice {
|
||||||
|
if fn(i, v) {
|
||||||
elemType := sv.Type().Elem()
|
res = append(res, v)
|
||||||
if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) {
|
|
||||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
|
||||||
}
|
|
||||||
|
|
||||||
res := reflect.MakeSlice(sv.Type(), 0, 0)
|
|
||||||
for i := 0; i < sv.Len(); i++ {
|
|
||||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
|
||||||
if flag.Bool() {
|
|
||||||
res = reflect.Append(res, sv.Index(i))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res
|
||||||
return res.Interface()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count iterates over elements of slice, returns a count of all matched elements
|
// Count iterates over elements of slice, returns a count of all matched elements
|
||||||
@@ -288,37 +278,22 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ForEach iterates over elements of slice and invokes function for each element
|
// ForEach iterates over elements of slice and invokes function for each element
|
||||||
// The function signature should be func(index int, value interface{}).
|
// The fn signature should be func(int, T ).
|
||||||
func ForEach(slice, function interface{}) {
|
func ForEach[T any] (slice []T, fn func(index int, t T)) {
|
||||||
sv := sliceValue(slice)
|
for i, v := range slice {
|
||||||
fn := functionValue(function)
|
fn(i, v)
|
||||||
|
|
||||||
elemType := sv.Type().Elem()
|
|
||||||
if checkSliceCallbackFuncSignature(fn, elemType, nil) {
|
|
||||||
panic("function param should be of type func(int, " + elemType.String() + ")" + elemType.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < sv.Len(); i++ {
|
|
||||||
fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map creates an slice of values by running each element of `slice` thru `function`.
|
// Map creates an slice of values by running each element of `slice` thru `function`.
|
||||||
// The function signature should be func(index int, value interface{}) interface{}.
|
// The fn signature should be func(int, T).
|
||||||
func Map(slice, function interface{}) interface{} {
|
func Map[T any, U any](slice []T, fn func(index int, t T) U) []U {
|
||||||
sv := sliceValue(slice)
|
res := make([]U, len(slice), cap(slice))
|
||||||
fn := functionValue(function)
|
for i, v := range slice {
|
||||||
|
res[i] = fn(i, v)
|
||||||
elemType := sv.Type().Elem()
|
|
||||||
if checkSliceCallbackFuncSignature(fn, elemType, nil) {
|
|
||||||
panic("function param should be of type func(int, " + elemType.String() + ")" + elemType.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res := reflect.MakeSlice(sv.Type(), sv.Len(), sv.Len())
|
return res
|
||||||
for i := 0; i < sv.Len(); i++ {
|
|
||||||
res.Index(i).Set(fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0])
|
|
||||||
}
|
|
||||||
return res.Interface()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce creates an slice of values by running each element of `slice` thru `function`.
|
// Reduce creates an slice of values by running each element of `slice` thru `function`.
|
||||||
|
|||||||
@@ -253,9 +253,11 @@ func TestForEach(t *testing.T) {
|
|||||||
expected := []int{3, 4, 5, 6, 7}
|
expected := []int{3, 4, 5, 6, 7}
|
||||||
|
|
||||||
var numbersAddTwo []int
|
var numbersAddTwo []int
|
||||||
ForEach(numbers, func(index int, value int) {
|
addTwo := func(index int, value int) {
|
||||||
numbersAddTwo = append(numbersAddTwo, value+2)
|
numbersAddTwo = append(numbersAddTwo, value+2)
|
||||||
})
|
}
|
||||||
|
|
||||||
|
ForEach(numbers, addTwo)
|
||||||
|
|
||||||
if !reflect.DeepEqual(numbersAddTwo, expected) {
|
if !reflect.DeepEqual(numbersAddTwo, expected) {
|
||||||
internal.LogFailedTestInfo(t, "ForEach", numbers, expected, numbersAddTwo)
|
internal.LogFailedTestInfo(t, "ForEach", numbers, expected, numbersAddTwo)
|
||||||
@@ -265,14 +267,16 @@ func TestForEach(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMap(t *testing.T) {
|
func TestMap(t *testing.T) {
|
||||||
s1 := []int{1, 2, 3, 4}
|
numbers := []int{1, 2, 3, 4}
|
||||||
multiplyTwo := func(i, num int) int {
|
multiplyTwo := func(i, num int) int {
|
||||||
return num * 2
|
return num * 2
|
||||||
}
|
}
|
||||||
e1 := []int{2, 4, 6, 8}
|
|
||||||
r1 := Map(s1, multiplyTwo)
|
expected := []int{2, 4, 6, 8}
|
||||||
if !reflect.DeepEqual(r1, e1) {
|
actual := Map(numbers, multiplyTwo)
|
||||||
internal.LogFailedTestInfo(t, "Map", s1, e1, r1)
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
internal.LogFailedTestInfo(t, "Map", numbers, expected, actual)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user