-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
372 lines (289 loc) · 8.66 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package lo
import (
"fmt"
"math/rand"
"golang.org/x/exp/constraints"
)
// import "golang.org/x/exp/constraints"
// IndexOf returns the index at which the first occurrence of a value is found in an array or return -1
// if the value cannot be found.
func IndexOf[T comparable](collection []T, element T) int {
for i, item := range collection {
if item == element {
return i
}
}
return -1
}
// LastIndexOf returns the index at which the last occurrence of a value is found in an array or return -1
// if the value cannot be found.
func LastIndexOf[T comparable](collection []T, element T) int {
length := len(collection)
for i := length - 1; i >= 0; i-- {
if collection[i] == element {
return i
}
}
return -1
}
// Find search an element in a slice based on a predicate. It returns element and true if element was found.
func Find[T any](collection []T, predicate func(item T) bool) (T, bool) {
for _, item := range collection {
if predicate(item) {
return item, true
}
}
var result T
return result, false
}
// FindIndexOf searches an element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool) {
for i, item := range collection {
if predicate(item) {
return item, i, true
}
}
var result T
return result, -1, false
}
// FindLastIndexOf searches last element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool) {
length := len(collection)
for i := length - 1; i >= 0; i-- {
if predicate(collection[i]) {
return collection[i], i, true
}
}
var result T
return result, -1, false
}
// FindOrElse search an element in a slice based on a predicate. It returns the element if found or a given fallback value otherwise.
func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T {
for _, item := range collection {
if predicate(item) {
return item
}
}
return fallback
}
// FindKey returns the key of the first value matching.
func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool) {
for k, v := range object {
if v == value {
return k, true
}
}
return Empty[K](), false
}
// FindKeyBy returns the key of the first element predicate returns truthy for.
func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool) {
for k, v := range object {
if predicate(k, v) {
return k, true
}
}
return Empty[K](), false
}
// FindUniques returns a slice with all the unique elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func FindUniques[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))
for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
}
}
result := make([]T, 0, len(collection)-len(isDupl))
for _, item := range collection {
if duplicated := isDupl[item]; !duplicated {
result = append(result, item)
}
}
return result
}
// FindUniquesBy returns a slice with all the unique elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindUniquesBy[T any, U comparable](collection []T, iteratee func(item T) U) []T {
isDupl := make(map[U]bool, len(collection))
for _, item := range collection {
key := iteratee(item)
duplicated, ok := isDupl[key]
if !ok {
isDupl[key] = false
} else if !duplicated {
isDupl[key] = true
}
}
result := make([]T, 0, len(collection)-len(isDupl))
for _, item := range collection {
key := iteratee(item)
if duplicated := isDupl[key]; !duplicated {
result = append(result, item)
}
}
return result
}
// FindDuplicates returns a slice with the first occurrence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func FindDuplicates[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))
for _, item := range collection {
duplicated, ok := isDupl[item]
if !ok {
isDupl[item] = false
} else if !duplicated {
isDupl[item] = true
}
}
result := make([]T, 0, len(collection)-len(isDupl))
for _, item := range collection {
if duplicated := isDupl[item]; duplicated {
result = append(result, item)
isDupl[item] = false
}
}
return result
}
// FindDuplicatesBy returns a slice with the first occurrence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(item T) U) []T {
isDupl := make(map[U]bool, len(collection))
for _, item := range collection {
key := iteratee(item)
duplicated, ok := isDupl[key]
if !ok {
isDupl[key] = false
} else if !duplicated {
isDupl[key] = true
}
}
result := make([]T, 0, len(collection)-len(isDupl))
for _, item := range collection {
key := iteratee(item)
if duplicated := isDupl[key]; duplicated {
result = append(result, item)
isDupl[key] = false
}
}
return result
}
// Min search the minimum value of a collection.
// Returns zero value when collection is empty.
func Min[T constraints.Ordered](collection []T) T {
var min T
if len(collection) == 0 {
return min
}
min = collection[0]
for i := 1; i < len(collection); i++ {
item := collection[i]
if item < min {
min = item
}
}
return min
}
// MinBy search the minimum value of a collection using the given comparison function.
// If several values of the collection are equal to the smallest value, returns the first such value.
// Returns zero value when collection is empty.
func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
var min T
if len(collection) == 0 {
return min
}
min = collection[0]
for i := 1; i < len(collection); i++ {
item := collection[i]
if comparison(item, min) {
min = item
}
}
return min
}
// Max searches the maximum value of a collection.
// Returns zero value when collection is empty.
func Max[T constraints.Ordered](collection []T) T {
var max T
if len(collection) == 0 {
return max
}
max = collection[0]
for i := 1; i < len(collection); i++ {
item := collection[i]
if item > max {
max = item
}
}
return max
}
// MaxBy search the maximum value of a collection using the given comparison function.
// If several values of the collection are equal to the greatest value, returns the first such value.
// Returns zero value when collection is empty.
func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
var max T
if len(collection) == 0 {
return max
}
max = collection[0]
for i := 1; i < len(collection); i++ {
item := collection[i]
if comparison(item, max) {
max = item
}
}
return max
}
// Last returns the last element of a collection or error if empty.
func Last[T any](collection []T) (T, error) {
length := len(collection)
if length == 0 {
var t T
return t, fmt.Errorf("last: cannot extract the last element of an empty slice")
}
return collection[length-1], nil
}
// Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element
// from the end is returned. An error is returned when nth is out of slice bounds.
func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error) {
n := int(nth)
l := len(collection)
if n >= l || -n > l {
var t T
return t, fmt.Errorf("nth: %d out of slice bounds", n)
}
if n >= 0 {
return collection[n], nil
}
return collection[l+n], nil
}
// Sample returns a random item from collection.
func Sample[T any](collection []T) T {
size := len(collection)
if size == 0 {
return Empty[T]()
}
return collection[rand.Intn(size)]
}
// Samples returns N random unique items from collection.
func Samples[T any](collection []T, count int) []T {
size := len(collection)
copy := append([]T{}, collection...)
results := []T{}
for i := 0; i < size && i < count; i++ {
copyLength := size - i
index := rand.Intn(size - i)
results = append(results, copy[index])
// Removes element.
// It is faster to swap with last element and remove it.
copy[index] = copy[copyLength-1]
copy = copy[:copyLength-1]
}
return results
}