-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
115 lines (107 loc) · 2.61 KB
/
find.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gotil
// FindBy iterates over elements of collection, returns an object of fist element predicate if returns true for.
// data := []int64{-100, -5, 30, 100}
// newData, _ := FindBy(data, func(val interface{}, i int) bool {
// if val.(int64) == 30 {
// return true
// } else {
// return false
// }
// })
// // Output: 30
func FindBy[T any](s []T, f func(val T) bool) T {
return FindByFromIndex(s, f, 0)
}
// FindByFromIndex iterates over elements of collection, returns an object of fist element predicate if returns true for.
// It works same as FindBy just from parameter provides to set start index of given slice or array.
func FindByFromIndex[T any](s []T, f func(val T) bool, from int) T {
var result T
index := findIndex(s, f, from)
if index > -1 {
return s[index]
}
return result
}
// FindLastBy iterates over elements of collection, returns an object of lastest element predicate if returns true for.
// data := []int64{-100, -5, 30, 100}
// newData, _ := FindLastBy(data, func(val interface{}, i int) bool {
// if val.(int64) == 30 {
// return true
// } else {
// return false
// }
// })
// // Output: 30
func FindLastBy[T any](s []T, f func(val T) bool) T {
return FindLastByFromIndex(s, f, 0)
}
// FindLastBy iterates over elements of collection, returns an object of lastest element predicate if returns true for.
// It works same as FindLastBy just from parameter provides to set start index of given slice or array.
func FindLastByFromIndex[T any](s []T, f func(val T) bool, from int) T {
var result T
index := findLastIndex(s, f, 0)
if index > -1 {
return s[index]
}
return result
}
func findIndex[T any](s []T, f func(val T) bool, from int) int {
length := len(s)
if length == 0 {
return -1
}
index := 0
if from < 0 {
index = max(length+index, 0)
} else {
index = min(index, length-1)
}
return baseFindIndex(s, f, index, false)
}
func findLastIndex[T any](s []T, f func(val T) bool, from int) int {
length := len(s)
if length == 0 {
return -1
}
index := length - 1
if from < 0 {
index = max(length+index, 0)
} else {
index = min(index, length-1)
}
return baseFindIndex(s, f, index, true)
}
func baseFindIndex[T any](s []T, f func(val T) bool, from int, right bool) int {
length := len(s)
index := from
if right {
for index < length {
value := s[index]
if f(value) {
return index
}
index--
}
} else {
for index < length {
value := s[index]
if f(value) {
return index
}
index++
}
}
return -1
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}