-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
find.go
191 lines (163 loc) · 4.02 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gogu
import (
"errors"
"fmt"
"golang.org/x/exp/constraints"
)
// FindIndex returns the index of the first found element.
func FindIndex[T any](s []T, fn func(T) bool) int {
for k, v := range s {
if fn(v) {
return k
}
}
return -1
}
// FindLastIndex is like FindIndex, only that returns the index of last found element.
func FindLastIndex[T any](s []T, fn func(T) bool) int {
for i, j := len(s)-1, 0; i >= 0; i, j = i-1, j+1 {
if fn(s[i]) {
return i
}
}
return -1
}
// FindAll is like FindIndex, but returns into a map all the values
// which satisfies the conditional logic of the callback function.
// The map key represents the position of the found value and the value is the item itself.
func FindAll[T any](s []T, fn func(T) bool) map[int]T {
m := make(map[int]T, len(s))
for k, v := range s {
if fn(v) {
m[k] = v
}
}
return m
}
// FindMin finds the minimum value of a slice.
func FindMin[T constraints.Ordered](s []T) T {
var min T
if len(s) > 0 {
min = s[0]
}
for i := 0; i < len(s); i++ {
if s[i] < min {
min = s[i]
}
}
return min
}
// FindMinBy is like FindMin except that it accept a callback function
// and the conditional logic is applied over the resulted value.
// If there are more than one identical values resulted
// from the callback function the first one is used.
func FindMinBy[T constraints.Ordered](s []T, fn func(val T) T) T {
var min T
if len(s) > 0 {
min = s[0]
}
for i := 0; i < len(s); i++ {
if fn(s[i]) < fn(min) {
min = s[i]
}
}
return min
}
// FindMinByKey finds the minimum value from a map by using some existing key as a parameter.
func FindMinByKey[K comparable, T constraints.Ordered](mapSlice []map[K]T, key K) (T, error) {
var min T
if _, ok := mapSlice[0][key]; !ok {
return min, errors.New("key not found")
}
if len(mapSlice) > 0 {
min = mapSlice[0][key]
}
for _, m := range mapSlice {
mapped := FindByKey(m, func(k K) bool {
return k == key
})
if _, ok := mapped[key]; ok {
if mapped[key] < min {
min = mapped[key]
}
}
}
return min, nil
}
// FindMax finds the maximum value of a slice.
func FindMax[T constraints.Ordered](s []T) T {
var max T
if len(s) > 0 {
max = s[0]
}
for i := 0; i < len(s); i++ {
if s[i] > max {
max = s[i]
}
}
return max
}
// FindMaxBy is like FindMax except that it accept a callback function
// and the conditional logic is applied over the resulted value.
// If there are more than one identical values resulted
// from the callback function the first one is returned.
func FindMaxBy[T constraints.Ordered](s []T, fn func(val T) T) T {
var max T
if len(s) > 0 {
max = s[0]
}
for i := 0; i < len(s); i++ {
if fn(s[i]) > fn(max) {
max = s[i]
}
}
return max
}
// FindMaxByKey finds the maximum value from a map by using some existing key as a parameter.
func FindMaxByKey[K comparable, T constraints.Ordered](mapSlice []map[K]T, key K) (T, error) {
var max T
if _, ok := mapSlice[0][key]; !ok {
return max, errors.New("key not found")
}
if len(mapSlice) > 0 {
max = mapSlice[0][key]
}
for _, m := range mapSlice {
mapped := FindByKey(m, func(k K) bool {
return k == key
})
if _, ok := mapped[key]; ok {
if mapped[key] > max {
max = mapped[key]
}
}
}
return max, nil
}
// Nth returns the nth element of the collection.
// In case of negative value the nth element is returned from the end of the collection.
// In case nth is out of bounds an error is returned.
func Nth[T any](slice []T, nth int) (T, error) {
bounds := Bound[int]{0, len(slice)}
if (nth > 0 && nth > bounds.Max-1) ||
(nth < 0 && bounds.Max-Abs(nth) < 0) {
var t T
return t, fmt.Errorf("%d out of slice bounds %d", nth, bounds.Max)
}
if bounds.Enclose(nth) {
if nth >= 0 {
return slice[nth], nil
}
}
return slice[len(slice)-Abs(nth)], nil
}
type Bound[T constraints.Signed] struct {
Min, Max T
}
// Enclose checks if an element is inside the bounds.
func (b Bound[T]) Enclose(nth T) bool {
if Abs(nth) >= b.Min && Abs(nth) <= b.Max {
return true
}
return false
}