-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
462 lines (394 loc) · 9.87 KB
/
slice.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Package gosc is an helper package for Go, written to be user friendly with alias and inspired by Lodash.
package gosc
import (
"fmt"
"math/rand"
"reflect"
"sort"
"strings"
"time"
)
// MapString a new slice containing the results of applying the function f to each string in the original slice.
func MapString(s []string, f func(string) string) []string {
sm := make([]string, len(s))
for i, v := range s {
sm[i] = f(v)
}
return sm
}
// MapInt a new slice containing the results of applying the function f to each int in the original slice.
func MapInt(s []int, f func(int) int) []int {
sm := make([]int, len(s))
for i, v := range s {
sm[i] = f(v)
}
return sm
}
// MapFloat a new slice containing the results of applying the function f to each float64 in the original slice.
func MapFloat(s []float64, f func(float64) float64) []float64 {
sm := make([]float64, len(s))
for i, v := range s {
sm[i] = f(v)
}
return sm
}
// FilterString returns a new slice containing all strings in the slice that satisfy the predicate f.
func FilterString(s []string, f func(string) bool) []string {
sf := make([]string, 0)
for _, v := range s {
if f(v) {
sf = append(sf, v)
}
}
return sf
}
// FilterInt returns a new slice containing all ints in the slice that satisfy the predicate f.
func FilterInt(s []int, f func(int) bool) []int {
sf := make([]int, 0)
for _, v := range s {
if f(v) {
sf = append(sf, v)
}
}
return sf
}
// FilterFloat returns a new slice containing all float64s in the slice that satisfy the predicate f.
func FilterFloat(s []float64, f func(float64) bool) []float64 {
sf := make([]float64, 0)
for _, v := range s {
if f(v) {
sf = append(sf, v)
}
}
return sf
}
// AllString returns true if all of the strings in the slice satisfy the predicate f
func AllString(s []string, f func(string) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// AllInt returns true if all of the ints in the slice satisfy the predicate f
func AllInt(s []int, f func(int) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// AllFloat returns true if all of the float64s in the slice satisfy the predicate f
func AllFloat(s []float64, f func(float64) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// EveryString returns true if all of the strings in the slice satisfy the predicate f
func EveryString(s []string, f func(string) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// EveryInt returns true if all of the ints in the slice satisfy the predicate f
func EveryInt(s []int, f func(int) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// EveryFloat returns true if all of the float64s in the slice satisfy the predicate f
func EveryFloat(s []float64, f func(float64) bool) bool {
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
// AnyString returns true if one of the strings in the slice satisfies the predicate f
func AnyString(s []string, f func(string) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// AnyInt returns true if one of the ints in the slice satisfies the predicate f
func AnyInt(s []int, f func(int) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// AnyFloat returns true if one of the float64s in the slice satisfies the predicate f
func AnyFloat(s []float64, f func(float64) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// SomeString returns true if one of the strings in the slice satisfies the predicate f
func SomeString(s []string, f func(string) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// SomeInt returns true if one of the ints in the slice satisfies the predicate f
func SomeInt(s []int, f func(int) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// SomeFloat returns true if one of the float64s in the slice satisfies the predicate f
func SomeFloat(s []float64, f func(float64) bool) bool {
for _, v := range s {
if f(v) {
return true
}
}
return false
}
// Index returns the index of an element in a slice or -1 if not found
func Index(s interface{}, t interface{}) int {
// Retrieve slices
sl := reflect.ValueOf(s).Elem()
if sl.Len() == 0 {
return -1
}
for i := 0; i < sl.Len(); i++ {
if sl.Index(i).Interface() == t {
return i
}
}
return -1
}
// Indexi returns the index of a string in a slice or -1 if not found. Case Insentive.
func Indexi(s []string, t string) int {
if len(s) == 0 || len(t) == 0 {
return -1
}
for i, v := range s {
if strings.ToLower(v) == strings.ToLower(t) {
return i
}
}
return -1
}
// Delete an item from a slice
func Delete(s interface{}, i int) {
// Retrieve slice
sl := reflect.ValueOf(s).Elem()
// Return if index not present or empty slice
if i > sl.Len()-1 || sl.Len() == 0 || i < 0 {
return
}
// Exit if not supported type
switch s.(type) {
case *[]string:
sli := sl.Interface().([]string)
*s.(*[]string) = append(sli[:i], sli[i+1:]...)
case *[]int:
sli := sl.Interface().([]int)
*s.(*[]int) = append(sli[:i], sli[i+1:]...)
case *[]float64:
sli := sl.Interface().([]float64)
*s.(*[]float64) = append(sli[:i], sli[i+1:]...)
default:
return
}
}
// Rsort reverses the order (desc) of a slice
func Rsort(s interface{}) {
// Retrieve slice
sl := reflect.ValueOf(s).Elem()
// Handle types or exit if not supported type
switch s.(type) {
case *[]string:
sli := sl.Interface().([]string)
sort.Sort(sort.Reverse(sort.StringSlice(sli)))
*s.(*[]string) = sli
case *[]int:
sli := sl.Interface().([]int)
sort.Sort(sort.Reverse(sort.IntSlice(sli)))
*s.(*[]int) = sli
case *[]float64:
sli := sl.Interface().([]float64)
sort.Sort(sort.Reverse(sort.Float64Slice(sli)))
*s.(*[]float64) = sli
default:
return
}
}
// ReverseSort is an alias of Rsort
func ReverseSort(s interface{}) {
// Retrieve slice
sl := reflect.ValueOf(s).Elem()
// Handle types or exit if not supported type
switch s.(type) {
case *[]string:
sli := sl.Interface().([]string)
sort.Sort(sort.Reverse(sort.StringSlice(sli)))
*s.(*[]string) = sli
case *[]int:
sli := sl.Interface().([]int)
sort.Sort(sort.Reverse(sort.IntSlice(sli)))
*s.(*[]int) = sli
case *[]float64:
sli := sl.Interface().([]float64)
sort.Sort(sort.Reverse(sort.Float64Slice(sli)))
*s.(*[]float64) = sli
default:
return
}
}
// EqSlices returns true if two slices are equal (not in-depth, for that use reflect.DeepEqual)
func EqSlices(a, b interface{}) bool {
// Retrieve slices
sl1 := reflect.ValueOf(a).Elem()
sl2 := reflect.ValueOf(b).Elem()
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if sl1.Len() != sl2.Len() {
return false
}
if fmt.Sprintf("%T", sl1.Interface()) != fmt.Sprintf("%T", sl1.Interface()) {
return false
}
for i := 0; i < sl1.Len(); i++ {
if sl1.Index(i).Interface() != sl2.Index(i).Interface() {
return false
}
}
return true
}
// SliceRand assign a random value from a string to the "r" arg
func SliceRand(s interface{}, r interface{}) {
// Initialize global pseudo random generator
rand.Seed(time.Now().Unix())
// Retrieve slice
sl := reflect.ValueOf(s).Elem()
// Handle types or exit if not supported type
switch s.(type) {
case *[]string:
sli := sl.Interface().([]string)
*r.(*string) = sli[rand.Intn(len(sli))]
case *[]int:
sli := sl.Interface().([]int)
*r.(*int) = sli[rand.Intn(len(sli))]
case *[]float64:
sli := sl.Interface().([]float64)
*r.(*float64) = sli[rand.Intn(len(sli))]
default:
return
}
}
// SliceRandString returns a random value from a string slice
func SliceRandString(s []string) string {
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
return s[rand.Intn(len(s))]
}
// SliceRandS is an alias of SliceRandString
func SliceRandS(s []string) string {
return SliceRandString(s)
}
// SliceRandInt returns a random value from an int slice
func SliceRandInt(s []int) int {
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
return s[rand.Intn(len(s))]
}
// SliceRandI is an alias of SliceRandInt
func SliceRandI(s []int) int {
return SliceRandInt(s)
}
// SliceRandFloat returns a random value from a float64 slice
func SliceRandFloat(s []float64) float64 {
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
return s[rand.Intn(len(s))]
}
// SliceRandF is an alias of SliceRandFloat
func SliceRandF(s []float64) float64 {
return SliceRandFloat(s)
}
// InSlice returns a boolean if the value is in the slice
// v = value to find
// s = slice
func InSlice(v interface{}, s interface{}) bool {
// Check if is passed as pointer
valueOf := reflect.ValueOf(s)
if valueOf.Kind() != reflect.Ptr {
panic("Non-pointer slice provided.")
}
// Retrieve slice
sl := valueOf.Elem()
// Invoke correct function
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
return inIntSlice(v.(int), sl)
case reflect.Float64:
return inFloat64Slice(v.(float64), sl)
case reflect.String:
return inStringSlice(v.(string), sl)
case reflect.Bool:
return inBoolSlice(v.(bool), sl)
default:
return false
}
}
func inIntSlice(v int, s reflect.Value) bool {
for i := 0; i < s.Len(); i++ {
if v == s.Index(i).Interface().(int) {
return true
}
}
return false
}
func inFloat64Slice(v float64, s reflect.Value) bool {
for i := 0; i < s.Len(); i++ {
if v == s.Index(i).Interface().(float64) {
return true
}
}
return false
}
func inStringSlice(v string, s reflect.Value) bool {
for i := 0; i < s.Len(); i++ {
if v == s.Index(i).Interface().(string) {
return true
}
}
return false
}
func inBoolSlice(v bool, s reflect.Value) bool {
for i := 0; i < s.Len(); i++ {
if v == s.Index(i).Interface().(bool) {
return true
}
}
return false
}