diff --git a/langext/array.go b/langext/array.go index 053d618..3caf34a 100644 --- a/langext/array.go +++ b/langext/array.go @@ -70,7 +70,73 @@ func ArrEqualsExact[T comparable](arr1 []T, arr2 []T) bool { return true } -func ArrAll(arr interface{}, fn func(int) bool) bool { +func ArrAll[T any](arr []T, fn func(T) bool) bool { + for _, av := range arr { + if !fn(av) { + return false + } + } + return true +} + +func ArrAllErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) { + for _, av := range arr { + v, err := fn(av) + if err != nil { + return false, err + } + if !v { + return false, nil + } + } + return true, nil +} + +func ArrNone[T any](arr []T, fn func(T) bool) bool { + for _, av := range arr { + if fn(av) { + return false + } + } + return true +} + +func ArrNoneErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) { + for _, av := range arr { + v, err := fn(av) + if err != nil { + return false, err + } + if v { + return false, nil + } + } + return true, nil +} + +func ArrAny[T any](arr []T, fn func(T) bool) bool { + for _, av := range arr { + if fn(av) { + return true + } + } + return false +} + +func ArrAnyErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) { + for _, av := range arr { + v, err := fn(av) + if err != nil { + return false, err + } + if v { + return true, nil + } + } + return false, nil +} + +func ArrIdxAll(arr any, fn func(int) bool) bool { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { if !fn(i) { @@ -80,7 +146,7 @@ func ArrAll(arr interface{}, fn func(int) bool) bool { return true } -func ArrAllErr(arr interface{}, fn func(int) (bool, error)) (bool, error) { +func ArrIdxAllErr(arr any, fn func(int) (bool, error)) (bool, error) { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { v, err := fn(i) @@ -94,7 +160,7 @@ func ArrAllErr(arr interface{}, fn func(int) (bool, error)) (bool, error) { return true, nil } -func ArrNone(arr interface{}, fn func(int) bool) bool { +func ArrIdxNone(arr any, fn func(int) bool) bool { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { if fn(i) { @@ -104,7 +170,7 @@ func ArrNone(arr interface{}, fn func(int) bool) bool { return true } -func ArrNoneErr(arr interface{}, fn func(int) (bool, error)) (bool, error) { +func ArrIdxNoneErr(arr any, fn func(int) (bool, error)) (bool, error) { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { v, err := fn(i) @@ -118,7 +184,7 @@ func ArrNoneErr(arr interface{}, fn func(int) (bool, error)) (bool, error) { return true, nil } -func ArrAny(arr interface{}, fn func(int) bool) bool { +func ArrIdxAny(arr any, fn func(int) bool) bool { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { if fn(i) { @@ -128,7 +194,7 @@ func ArrAny(arr interface{}, fn func(int) bool) bool { return false } -func ArrAnyErr(arr interface{}, fn func(int) (bool, error)) (bool, error) { +func ArrIdxAnyErr(arr any, fn func(int) (bool, error)) (bool, error) { av := reflect.ValueOf(arr) for i := 0; i < av.Len(); i++ { v, err := fn(i) @@ -142,7 +208,7 @@ 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) { +func ArrFirst[T any](arr []T, comp func(v T) bool) (T, bool) { for _, v := range arr { if comp(v) { return v, true @@ -151,7 +217,7 @@ func ArrFirst[T comparable](arr []T, comp func(v T) bool) (T, bool) { return *new(T), false } -func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) { +func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) { found := false result := *new(T) for _, v := range arr {