Add langext.ArrFirst / langext.ArrLast

This commit is contained in:
Mike Schwörer 2022-10-27 17:09:48 +02:00
parent 4dd1c08e77
commit 5dc9e98f6b
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF

View File

@ -142,6 +142,27 @@ func ArrAnyErr(arr interface{}, fn func(int) (bool, error)) (bool, error) {
return false, nil
}
func ArrFirst[T comparable](arr []T, comp func(v T) bool) (T, bool) {
for _, v := range arr {
if comp(v) {
return v, true
}
}
return *new(T), false
}
func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
found := false
result := *new(T)
for _, v := range arr {
if comp(v) {
found = true
result = v
}
}
return result, found
}
func AddToSet[T comparable](set []T, add T) []T {
for _, v := range set {
if v == add {