@@ -10,6 +10,10 @@ import (
10
10
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
11
11
// Play: https://go.dev/play/p/Apjg3WeSi7K
12
12
func Filter [T any , Slice ~ []T ](collection Slice , predicate func (item T , index int ) bool ) Slice {
13
+ if collection == nil {
14
+ return nil
15
+ }
16
+
13
17
result := make (Slice , 0 , len (collection ))
14
18
15
19
for i := range collection {
@@ -24,6 +28,10 @@ func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index in
24
28
// Map manipulates a slice and transforms it to a slice of another type.
25
29
// Play: https://go.dev/play/p/OkPcYAhBo0D
26
30
func Map [T any , R any ](collection []T , iteratee func (item T , index int ) R ) []R {
31
+ if collection == nil {
32
+ return nil
33
+ }
34
+
27
35
result := make ([]R , len (collection ))
28
36
29
37
for i := range collection {
@@ -40,6 +48,10 @@ func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
40
48
//
41
49
// Play: https://go.dev/play/p/-AuYXfy7opz
42
50
func FilterMap [T any , R any ](collection []T , callback func (item T , index int ) (R , bool )) []R {
51
+ if collection == nil {
52
+ return nil
53
+ }
54
+
43
55
result := []R {}
44
56
45
57
for i := range collection {
@@ -56,6 +68,10 @@ func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R
56
68
// no value is added to the final slice.
57
69
// Play: https://go.dev/play/p/YSoYmQTA8-U
58
70
func FlatMap [T any , R any ](collection []T , iteratee func (item T , index int ) []R ) []R {
71
+ if collection == nil {
72
+ return nil
73
+ }
74
+
59
75
result := make ([]R , 0 , len (collection ))
60
76
61
77
for i := range collection {
@@ -111,6 +127,10 @@ func Times[T any](count int, iteratee func(index int) T) []T {
111
127
// The order of result values is determined by the order they occur in the array.
112
128
// Play: https://go.dev/play/p/DTzbeXZ6iEN
113
129
func Uniq [T comparable , Slice ~ []T ](collection Slice ) Slice {
130
+ if collection == nil {
131
+ return nil
132
+ }
133
+
114
134
result := make (Slice , 0 , len (collection ))
115
135
seen := make (map [T ]struct {}, len (collection ))
116
136
@@ -131,6 +151,10 @@ func Uniq[T comparable, Slice ~[]T](collection Slice) Slice {
131
151
// invoked for each element in array to generate the criterion by which uniqueness is computed.
132
152
// Play: https://go.dev/play/p/g42Z3QSb53u
133
153
func UniqBy [T any , U comparable , Slice ~ []T ](collection Slice , iteratee func (item T ) U ) Slice {
154
+ if collection == nil {
155
+ return nil
156
+ }
157
+
134
158
result := make (Slice , 0 , len (collection ))
135
159
seen := make (map [U ]struct {}, len (collection ))
136
160
@@ -170,6 +194,10 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
170
194
panic ("Second parameter must be greater than 0" )
171
195
}
172
196
197
+ if collection == nil {
198
+ return nil
199
+ }
200
+
173
201
chunksNum := len (collection ) / size
174
202
if len (collection )% size != 0 {
175
203
chunksNum += 1
@@ -193,6 +221,10 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
193
221
// of running each element of collection through iteratee.
194
222
// Play: https://go.dev/play/p/NfQ_nGjkgXW
195
223
func PartitionBy [T any , K comparable , Slice ~ []T ](collection Slice , iteratee func (item T ) K ) []Slice {
224
+ if collection == nil {
225
+ return nil
226
+ }
227
+
196
228
result := []Slice {}
197
229
seen := map [K ]int {}
198
230
@@ -219,6 +251,10 @@ func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee fun
219
251
// Flatten returns an array a single level deep.
220
252
// Play: https://go.dev/play/p/rbp9ORaMpjw
221
253
func Flatten [T any , Slice ~ []T ](collection []Slice ) Slice {
254
+ if collection == nil {
255
+ return nil
256
+ }
257
+
222
258
totalLen := 0
223
259
for i := range collection {
224
260
totalLen += len (collection [i ])
@@ -283,6 +319,10 @@ func Shuffle[T any, Slice ~[]T](collection Slice) Slice {
283
319
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
284
320
// Play: https://go.dev/play/p/fhUMLvZ7vS6
285
321
func Reverse [T any , Slice ~ []T ](collection Slice ) Slice {
322
+ if collection == nil {
323
+ return nil
324
+ }
325
+
286
326
length := len (collection )
287
327
half := length / 2
288
328
@@ -297,6 +337,10 @@ func Reverse[T any, Slice ~[]T](collection Slice) Slice {
297
337
// Fill fills elements of array with `initial` value.
298
338
// Play: https://go.dev/play/p/VwR34GzqEub
299
339
func Fill [T Clonable [T ]](collection []T , initial T ) []T {
340
+ if collection == nil {
341
+ return nil
342
+ }
343
+
300
344
result := make ([]T , 0 , len (collection ))
301
345
302
346
for range collection {
@@ -370,6 +414,10 @@ func SliceToMap[T any, K comparable, V any](collection []T, transform func(item
370
414
// Drop drops n elements from the beginning of a slice or array.
371
415
// Play: https://go.dev/play/p/JswS7vXRJP2
372
416
func Drop [T any , Slice ~ []T ](collection Slice , n int ) Slice {
417
+ if collection == nil {
418
+ return nil
419
+ }
420
+
373
421
if len (collection ) <= n {
374
422
return make (Slice , 0 )
375
423
}
@@ -382,6 +430,10 @@ func Drop[T any, Slice ~[]T](collection Slice, n int) Slice {
382
430
// DropRight drops n elements from the end of a slice or array.
383
431
// Play: https://go.dev/play/p/GG0nXkSJJa3
384
432
func DropRight [T any , Slice ~ []T ](collection Slice , n int ) Slice {
433
+ if collection == nil {
434
+ return nil
435
+ }
436
+
385
437
if len (collection ) <= n {
386
438
return Slice {}
387
439
}
@@ -393,6 +445,10 @@ func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice {
393
445
// DropWhile drops elements from the beginning of a slice or array while the predicate returns true.
394
446
// Play: https://go.dev/play/p/7gBPYw2IK16
395
447
func DropWhile [T any , Slice ~ []T ](collection Slice , predicate func (item T ) bool ) Slice {
448
+ if collection == nil {
449
+ return nil
450
+ }
451
+
396
452
i := 0
397
453
for ; i < len (collection ); i ++ {
398
454
if ! predicate (collection [i ]) {
@@ -407,6 +463,10 @@ func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool)
407
463
// DropRightWhile drops elements from the end of a slice or array while the predicate returns true.
408
464
// Play: https://go.dev/play/p/3-n71oEC0Hz
409
465
func DropRightWhile [T any , Slice ~ []T ](collection Slice , predicate func (item T ) bool ) Slice {
466
+ if collection == nil {
467
+ return nil
468
+ }
469
+
410
470
i := len (collection ) - 1
411
471
for ; i >= 0 ; i -- {
412
472
if ! predicate (collection [i ]) {
@@ -453,6 +513,10 @@ func DropByIndex[T any](collection []T, indexes ...int) []T {
453
513
// Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.
454
514
// Play: https://go.dev/play/p/YkLMODy1WEL
455
515
func Reject [T any , Slice ~ []T ](collection Slice , predicate func (item T , index int ) bool ) Slice {
516
+ if collection == nil {
517
+ return nil
518
+ }
519
+
456
520
result := Slice {}
457
521
458
522
for i := range collection {
@@ -549,6 +613,10 @@ func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) m
549
613
// Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow.
550
614
// Play: https://go.dev/play/p/tOQu1GhFcog
551
615
func Subset [T any , Slice ~ []T ](collection Slice , offset int , length uint ) Slice {
616
+ if collection == nil {
617
+ return nil
618
+ }
619
+
552
620
size := len (collection )
553
621
554
622
if offset < 0 {
@@ -572,6 +640,10 @@ func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice
572
640
// Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow.
573
641
// Play: https://go.dev/play/p/8XWYhfMMA1h
574
642
func Slice [T any , Slice ~ []T ](collection Slice , start int , end int ) Slice {
643
+ if collection == nil {
644
+ return nil
645
+ }
646
+
575
647
size := len (collection )
576
648
577
649
if start >= end {
@@ -598,7 +670,11 @@ func Slice[T any, Slice ~[]T](collection Slice, start int, end int) Slice {
598
670
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
599
671
// Play: https://go.dev/play/p/XfPzmf9gql6
600
672
func Replace [T comparable , Slice ~ []T ](collection Slice , old T , new T , n int ) Slice {
601
- result := make (Slice , len (collection ))
673
+ if collection == nil {
674
+ return nil
675
+ }
676
+
677
+ result := make ([]T , len (collection ))
602
678
copy (result , collection )
603
679
604
680
for i := range result {
@@ -620,6 +696,10 @@ func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old T, new T) Slice
620
696
// Compact returns a slice of all non-zero elements.
621
697
// Play: https://go.dev/play/p/tXiy-iK6PAc
622
698
func Compact [T comparable , Slice ~ []T ](collection Slice ) Slice {
699
+ if collection == nil {
700
+ return nil
701
+ }
702
+
623
703
var zero T
624
704
625
705
result := make (Slice , 0 , len (collection ))
0 commit comments