41 lines
898 B
Go
41 lines
898 B
Go
|
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
|
||
|
}
|
||
|
|
||
|
func Map[I, O any](slice []I, mapper func(I) O) []O {
|
||
|
ret := make([]O, 0, len(slice))
|
||
|
for _, v := range slice {
|
||
|
ret = append(ret, mapper(v))
|
||
|
}
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func Each[T any](slice []T, f func(T)) {
|
||
|
EachIndex(slice, func(_ int, v T) { f(v) })
|
||
|
}
|
||
|
|
||
|
func EachIndex[T any](slice []T, f func(int, T)) {
|
||
|
for i, v := range slice {
|
||
|
f(i, v)
|
||
|
}
|
||
|
}
|