1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 14:12:28 +08:00

add func IndexOf and LastIndexOf

This commit is contained in:
dudaodong
2022-05-16 10:40:33 +08:00
parent eff2f22440
commit cc6e10ee5a

View File

@@ -733,3 +733,27 @@ func Without[T any](slice []T, values ...T) []T {
return out
}
// IndexOf returns the index at which the first occurrence of a value is found in a slice or return -1
// if the value cannot be found.
func IndexOf[T any](slice []T, value T) int {
for i, v := range slice {
if reflect.DeepEqual(v, value) {
return i
}
}
return -1
}
// LastIndexOf returns the index at which the last occurrence of a value is found in a slice or return -1
// if the value cannot be found.
func LastIndexOf[T any](slice []T, value T) int {
for i := len(slice) - 1; i > 0; i-- {
if reflect.DeepEqual(value, slice[i]) {
return i
}
}
return -1
}