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

feat: add DropRightWhile

This commit is contained in:
dudaodong
2023-02-09 17:53:26 +08:00
parent 97447d058e
commit d231d9f572
3 changed files with 60 additions and 0 deletions

View File

@@ -610,6 +610,22 @@ func DropWhile[T any](slice []T, predicate func(item T) bool) []T {
return append(result, slice[i:]...)
}
// DropRightWhile drop n elements from the end of a slice while predicate function returns true.
// Play: todo
func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T {
i := len(slice) - 1
for ; i >= 0; i-- {
if !predicate(slice[i]) {
break
}
}
result := make([]T, 0, i+1)
return append(result, slice[:i+1]...)
}
// InsertAt insert the value or other slice into slice at index.
// Play: https://go.dev/play/p/hMLNxPEGJVE
func InsertAt[T any](slice []T, index int, value any) []T {