mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 01:02:28 +08:00
feat: add FindLastBy function
This commit is contained in:
@@ -367,6 +367,26 @@ func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok b
|
|||||||
return slice[index], true
|
return slice[index], true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindLastBy iterates over elements of slice, returning the last one that passes a truth test on predicate function.
|
||||||
|
// If return T is nil or zero value then no items matched the predicate func.
|
||||||
|
// In contrast to Find or FindLast, its return value no longer requires dereferencing
|
||||||
|
func FindLastBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
|
||||||
|
index := -1
|
||||||
|
|
||||||
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
|
if predicate(i, slice[i]) {
|
||||||
|
index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if index == -1 {
|
||||||
|
return v, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return slice[index], true
|
||||||
|
}
|
||||||
|
|
||||||
// Flatten flattens slice with one level.
|
// Flatten flattens slice with one level.
|
||||||
// Play: https://go.dev/play/p/hYa3cBEevtm
|
// Play: https://go.dev/play/p/hYa3cBEevtm
|
||||||
func Flatten(slice any) any {
|
func Flatten(slice any) any {
|
||||||
|
|||||||
@@ -358,6 +358,23 @@ func ExampleFindBy() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleFindLastBy() {
|
||||||
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
isEven := func(i, num int) bool {
|
||||||
|
return num%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
result, ok := FindLastBy(nums, isEven)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleFlatten() {
|
func ExampleFlatten() {
|
||||||
arrs := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
arrs := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
|
|
||||||
|
|||||||
@@ -268,6 +268,25 @@ func TestFindBy(t *testing.T) {
|
|||||||
assert.Equal(res == 0 && ok == false, true)
|
assert.Equal(res == 0 && ok == false, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindLastBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFindBy")
|
||||||
|
|
||||||
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
|
even := func(i, num int) bool {
|
||||||
|
return num%2 == 0
|
||||||
|
}
|
||||||
|
res, ok := FindLastBy(nums, even)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("found nothing")
|
||||||
|
}
|
||||||
|
assert.Equal(4, res)
|
||||||
|
|
||||||
|
res, ok = FindLastBy(nums, func(_ int, v int) bool {
|
||||||
|
return v == 6
|
||||||
|
})
|
||||||
|
assert.Equal(res == 0 && ok == false, true)
|
||||||
|
}
|
||||||
|
|
||||||
func TestFindLast(t *testing.T) {
|
func TestFindLast(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
even := func(i, num int) bool {
|
even := func(i, num int) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user