Find Index by Value
Find the index of a value in by applying a predicate on each value.
Examples in
Go
package main
import (
"fmt"
"strings"
)
func FindStrIdx(list []string, pred func(s string) bool) int {
for i, s := range list {
if pred(s) {
return i
}
}
return -1
}
func main() {
list := []string{"slice", "of", "strings"}
res := FindStrIdx(list, func(s string) bool {
return strings.HasPrefix(s, "o")
})
fmt.Println(res)
}
Last Run
:
1