initial commit
This commit is contained in:
39
pkg/array/any.go
Normal file
39
pkg/array/any.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package array
|
||||
|
||||
func All[T any](arr []T, predicate func(val T) bool) bool {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if !predicate(arr[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Any returns true if any element in the array satisfies the predicate; otherwise, it returns false.
|
||||
func Any[TIn any](arr []TIn, predicate func(val TIn) bool) bool {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if predicate(arr[i]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func AnyError[TIn any](arr []TIn, predicate func(val TIn) error) error {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if err := predicate(arr[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Contains checks if a slice contains a specific element.
|
||||
func Contains[T comparable](slice []T, element T) bool {
|
||||
for i := 0; i < len(slice); i++ {
|
||||
if slice[i] == element {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user