This commit is contained in:
Mike Schwörer 2023-04-04 17:10:38 +02:00
parent 2550691e2e
commit c266d9204b
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -217,6 +217,15 @@ func ArrFirst[T any](arr []T, comp func(v T) bool) (T, bool) {
return *new(T), false
}
func ArrFirstOrNil[T any](arr []T, comp func(v T) bool) *T {
for _, v := range arr {
if comp(v) {
return Ptr(v)
}
}
return nil
}
func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) {
found := false
result := *new(T)
@ -229,6 +238,22 @@ func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) {
return result, found
}
func ArrLastOrNil[T any](arr []T, comp func(v T) bool) *T {
found := false
result := *new(T)
for _, v := range arr {
if comp(v) {
found = true
result = v
}
}
if found {
return Ptr(result)
} else {
return nil
}
}
func ArrFirstIndex[T comparable](arr []T, needle T) int {
for i, v := range arr {
if v == needle {