more helper functions
This commit is contained in:
22
contains.go
Normal file
22
contains.go
Normal file
@ -0,0 +1,22 @@
|
||||
package slices
|
||||
|
||||
func IndexOf[T comparable](slice []T, value T) int {
|
||||
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func IndexOfCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
|
||||
for i, v := range slice {
|
||||
if cmp(v, value) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func Contains[T comparable](slice []T, value T) bool {
|
||||
return ContainsCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool {
|
||||
return IndexOfCmp(slice, value, cmp) != -1
|
||||
}
|
Reference in New Issue
Block a user