Add langext.ArrFirst / langext.ArrLast
This commit is contained in:
parent
4dd1c08e77
commit
5dc9e98f6b
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user