1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 15:52:27 +08:00

Slice: find v2 (#17)

* If slice len is zero then return false
* Convert find to generic form.
* If return T is nil then no items matched the predicate func
This commit is contained in:
donutloop
2022-01-07 13:19:27 +01:00
committed by GitHub
parent 3e9a2b5c59
commit 30d798366b
4 changed files with 14 additions and 19 deletions

View File

@@ -167,7 +167,7 @@ func Some(slice, function interface{}) bool {
// Filter iterates over elements of slice, returning an slice of all elements `signature` returns truthy for.
// The fn signature should be func(int, T) bool.
func Filter [T any] (slice []T, fn func(index int, t T) bool) []T {
func Filter[T any](slice []T, fn func(index int, t T) bool) []T {
res := make([]T, 0, 0)
for i, v := range slice {
if fn(i, v) {
@@ -227,30 +227,25 @@ func GroupBy(slice, function interface{}) (interface{}, interface{}) {
// Find iterates over elements of slice, returning the first one that passes a truth test on function.
// The function signature should be func(index int, value interface{}) bool .
func Find(slice, function interface{}) (interface{}, bool) {
sv := sliceValue(slice)
fn := functionValue(function)
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 return T is nil then no items matched the predicate func
func Find[T any](slice []T, fn func(index int, t T) bool) (*T, bool) {
if len(slice) == 0 {
return nil, false
}
index := -1
for i := 0; i < sv.Len(); i++ {
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
if flag.Bool() {
for i, v := range slice {
if fn(i, v) {
index = i
break
}
}
if index == -1 {
var none interface{}
return none, false
return nil, false
}
return sv.Index(index).Interface(), true
return &slice[index], true
}
// FlattenDeep flattens slice recursive
@@ -279,7 +274,7 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
// ForEach iterates over elements of slice and invokes function for each element
// The fn signature should be func(int, T ).
func ForEach [T any] (slice []T, fn func(index int, t T)) {
func ForEach[T any](slice []T, fn func(index int, t T)) {
for i, v := range slice {
fn(i, v)
}
@@ -287,7 +282,7 @@ func ForEach [T any] (slice []T, fn func(index int, t T)) {
// Map creates an slice of values by running each element of `slice` thru `function`.
// The fn signature should be func(int, T).
func Map [T any, U any] (slice []T, fn func(index int, t T) U) []U {
func Map[T any, U any](slice []T, fn func(index int, t T) U) []U {
res := make([]U, len(slice), cap(slice))
for i, v := range slice {
res[i] = fn(i, v)

View File

@@ -220,7 +220,7 @@ func TestFind(t *testing.T) {
t.Fatal("found nothing")
}
if res != 2 {
if *res != 2 {
internal.LogFailedTestInfo(t, "Find", nums, 2, res)
t.FailNow()
}