1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

feat: add FindLast func

This commit is contained in:
dudaodong
2022-01-24 15:22:43 +08:00
parent 18ec73839b
commit 4eaa0a39d5
4 changed files with 50 additions and 6 deletions

View File

@@ -257,10 +257,10 @@ func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) {
return groupA, groupB
}
// Find iterates over elements of slice, returning the first one that passes a truth test on iteratee function.
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
// If return T is nil then no items matched the predicate func
func Find[T any](slice []T, iteratee func(index int, t T) bool) (*T, bool) {
if iteratee == nil {
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
if predicate == nil {
panic("fn is missing")
}
@@ -270,7 +270,33 @@ func Find[T any](slice []T, iteratee func(index int, t T) bool) (*T, bool) {
index := -1
for i, v := range slice {
if iteratee(i, v) {
if predicate(i, v) {
index = i
break
}
}
if index == -1 {
return nil, false
}
return &slice[index], true
}
// FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function.
// If return T is nil then no items matched the predicate func
func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
if predicate == nil {
panic("fn is missing")
}
if len(slice) == 0 {
return nil, false
}
index := -1
for i := len(slice) - 1; i >= 0; i-- {
if predicate(i, slice[i]) {
index = i
break
}

View File

@@ -166,6 +166,20 @@ func TestFind(t *testing.T) {
assert.Equal(2, *res)
}
func TestFindLast(t *testing.T) {
nums := []int{2, 3, 4, 5}
even := func(i, num int) bool {
return num%2 == 0
}
res, ok := FindLast(nums, even)
if !ok {
t.Fatal("found nothing")
}
assert := internal.NewAssert(t, "TestFindLast")
assert.Equal(4, *res)
}
func TestFindFoundNothing(t *testing.T) {
nums := []int{1, 1, 1, 1, 1, 1}
findFunc := func(i, num int) bool {