-
Notifications
You must be signed in to change notification settings - Fork 2
/
011-findLast.go
40 lines (38 loc) · 988 Bytes
/
011-findLast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import "fmt"
func FindLastInt(arr []int, f func(int) bool) (int, error) {
for i := len(arr) - 1; i >= 0; i-- {
if f(arr[i]) {
return arr[i], nil
}
}
return 0, fmt.Errorf("No matches found")
}
func FindLastFloat64(arr []float64, f func(float64) bool) (float64, error) {
for i := len(arr) - 1; i >= 0; i-- {
if f(arr[i]) {
return arr[i], nil
}
}
return 0.0, fmt.Errorf("No matches found")
}
func FindLastBool(arr []bool, f func(bool) bool) (bool, error) {
for i := len(arr) - 1; i >= 0; i-- {
if f(arr[i]) {
return arr[i], nil
}
}
return false, fmt.Errorf("No matches found")
}
func FindLastString(arr []string, f func(string) bool) (string, error) {
for i := len(arr) - 1; i >= 0; i-- {
if f(arr[i]) {
return arr[i], nil
}
}
return "", fmt.Errorf("No matches found")
}
func main() {
r1, ch1 := FindLastInt([]int{1, 1, 2}, func(x int) bool { return x%2 == 0 }) // 2 nil
fmt.Println(r1, ch1)
}