1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +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

@@ -439,12 +439,14 @@ func Compact[T any](slice []T) []T //creates an slice with all falsey values rem
func Concat[T any](slice []T, values ...[]T) []T //creates a new slice concatenating slice with any additional slices and/or values
func Difference[T comparable](slice1, slice2 []T) []T //creates an slice of whose element not included in the other given slice
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared.
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //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.
func DeleteByIndex[T any](slice []T, start int, end ...int) []T //delete the element of slice from start index to end index - 1
func Drop[T any](slice []T, n int) []T //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
func Every[T any](slice []T, predicate func(index int, t T) bool) bool //return true if all of the values in the slice pass the predicate function
func None[T any](slice []T, predicate func(index int, t T) bool) bool // return true if all the values in the slice mismatch the criteria
func Filter [T any] (slice []T, predicate func(index int, t T) bool) []T //iterates over elements of slice, returning an slice of all elements pass the predicate function
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //iterates over elements of slice, returning the first one that passes a truth test on iteratee 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) //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 FlattenDeep(slice interface{}) interface{} //flattens slice recursive
func ForEach [T any] (slice []T, iteratee func(index int, t T)) //iterates over elements of slice and invokes function for each element
func IntSlice(slice interface{}) ([]int, error) //convert value to int slice

View File

@@ -438,12 +438,14 @@ func Chunk[T any](slice []T, size int) [][]T //均分slice
func Compact[T any](slice []T) []T //去除slice中的false vule. false values are false, nil, 0, and ""
func Concat[T any](slice []T, values ...[]T) []T //连接values到slice中
func Difference[T comparable](slice1, slice2 []T) []T //返回切片其元素在slice1中不在slice2中
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较如果不相等返回slice中的元素
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T //将slice 和comparedSlice中每个元素调用iterateeFn后作比较如果不相等返回slice中的元素
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T //将slice 和comparedSlice中每个元素调用comparator后作比较如果为false返回slice中的元素
func DeleteByIndex[T any](slice []T, start int, end ...int) []T //删除切片中start到end位置的值(不包含end)
func Drop[T any](slice []T, n int) []T //创建一个新切片当n大于0时删除原切片前n个元素当n小于0时删除原切片后n个元素
func Every[T any](slice []T, predicate func(index int, t T) bool) bool//slice中所有元素都符合函数条件时返回true, 否则返回false
func None[T any](slice []T, predicate func(index int, t T) bool) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //查找slice中第一个符合条件的元素没有找到时返回nil
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //查找slice中第一个符合条件的元素没有找到时返回nil, false
func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) //从后往前查找slice中第一个符合条件的元素没有找到时返回nil, false
func Filter [T any] (slice []T, predicate func(index int, t T) bool) []T//过滤slice中元素符合predicate函数
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片
func ForEach [T any] (slice []T, iteratee func(index int, t T)) //遍历切片在每个元素上执行iteratee函数

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 {