forked from elliotchance/pie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
1156 lines (988 loc) · 25.3 KB
/
template.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by go generate; DO NOT EDIT.
package main
var pieTemplates = map[string]string{
"Abs": `package functions
// Abs is a function which returns the absolute value of all the
// elements in the slice.
func (ss SliceType) Abs() SliceType {
result := make(SliceType, len(ss))
for i, val := range ss {
if val < 0 {
result[i] = -val
} else {
result[i] = val
}
}
return result
}
`,
"All": `package functions
// All will return true if all callbacks return true. It follows the same logic
// as the all() function in Python.
//
// If the list is empty then true is always returned.
func (ss SliceType) All(fn func(value ElementType) bool) bool {
for _, value := range ss {
if !fn(value) {
return false
}
}
return true
}
`,
"Any": `package functions
// Any will return true if any callbacks return true. It follows the same logic
// as the any() function in Python.
//
// If the list is empty then false is always returned.
func (ss SliceType) Any(fn func(value ElementType) bool) bool {
for _, value := range ss {
if fn(value) {
return true
}
}
return false
}
`,
"Append": `package functions
// Append will return a new slice with the elements appended to the end.
//
// It is acceptable to provide zero arguments.
func (ss SliceType) Append(elements ...ElementType) SliceType {
// Copy ss, to make sure no memory is overlapping between input and
// output. See issue #97.
result := append(SliceType{}, ss...)
result = append(result, elements...)
return result
}
`,
"AreSorted": `package functions
import (
"sort"
)
// AreSorted will return true if the slice is already sorted. It is a wrapper
// for sort.SliceTypeAreSorted.
func (ss SliceType) AreSorted() bool {
return sort.SliceIsSorted(ss, func(i, j int) bool {
return ss[i] < ss[j]
})
}
`,
"AreUnique": `package functions
// AreUnique will return true if the slice contains elements that are all
// different (unique) from each other.
func (ss SliceType) AreUnique() bool {
return ss.Unique().Len() == ss.Len()
}
`,
"Average": `package functions
// Average is the average of all of the elements, or zero if there are no
// elements.
func (ss SliceType) Average() float64 {
if l := ElementType(len(ss)); l > 0 {
return float64(ss.Sum()) / float64(l)
}
return 0
}
`,
"Bottom": `package functions
// Bottom will return n elements from bottom
//
// that means that elements is taken from the end of the slice
// for this [1,2,3] slice with n == 2 will be returned [3,2]
// if the slice has less elements then n that'll return all elements
// if n < 0 it'll return empty slice.
func (ss SliceType) Bottom(n int) (top SliceType) {
var lastIndex = len(ss) - 1
for i := lastIndex; i > -1 && n > 0; i-- {
top = append(top, ss[i])
n--
}
return
}
`,
"Contains": `package functions
// Contains returns true if the element exists in the slice.
//
// When using slices of pointers it will only compare by address, not value.
func (ss SliceType) Contains(lookingFor ElementType) bool {
for _, s := range ss {
if lookingFor.Equals(s) {
return true
}
}
return false
}
`,
"Diff": `package functions
// Diff returns the elements that needs to be added or removed from the first
// slice to have the same elements in the second slice.
//
// The order of elements is not taken into consideration, so the slices are
// treated sets that allow duplicate items.
//
// The added and removed returned may be blank respectively, or contain upto as
// many elements that exists in the largest slice.
func (ss SliceType) Diff(against SliceType) (added, removed SliceType) {
// This is probably not the best way to do it. We do an O(n^2) between the
// slices to see which items are missing in each direction.
diffOneWay := func(ss1, ss2raw SliceType) (result SliceType) {
ss2 := make(SliceType, len(ss2raw))
copy(ss2, ss2raw)
for _, s := range ss1 {
found := false
for i, element := range ss2 {
if s.Equals(element) {
ss2 = append(ss2[:i], ss2[i+1:]...)
found = true
}
}
if !found {
result = append(result, s)
}
}
return
}
removed = diffOneWay(ss, against)
added = diffOneWay(against, ss)
return
}
`,
"DropTop": `package functions
// DropTop will return the rest slice after dropping the top n elements
// if the slice has less elements then n that'll return empty slice
// if n < 0 it'll return empty slice.
func (ss SliceType) DropTop(n int) (drop SliceType) {
if n < 0 || n >= len(ss) {
return
}
// Copy ss, to make sure no memory is overlapping between input and
// output. See issue #145.
drop = make([]ElementType, len(ss)-n)
copy(drop, ss[n:])
return
}
`,
"Each": `package functions
// Each is more condensed version of Transform that allows an action to happen
// on each elements and pass the original slice on.
//
// cars.Each(func (car *Car) {
// fmt.Printf("Car color is: %s\n", car.Color)
// })
//
// Pie will not ensure immutability on items passed in so they can be
// manipulated, if you choose to do it this way, for example:
//
// // Set all car colors to Red.
// cars.Each(func (car *Car) {
// car.Color = "Red"
// })
//
func (ss SliceType) Each(fn func(ElementType)) SliceType {
for _, s := range ss {
fn(s)
}
return ss
}
`,
"Equals": `package functions
// Equals compare elements from the start to the end,
//
// if they are the same is considered the slices are equal if all elements are the same is considered the slices are equal
// if each slice == nil is considered that they're equal
//
// if element realizes Equals interface it uses that method, in other way uses default compare
func (ss SliceType) Equals(rhs SliceType) bool {
if len(ss) != len(rhs) {
return false
}
for i := range ss {
if !ss[i].Equals(rhs[i]) {
return false
}
}
return true
}
`,
"Extend": `package functions
// Extend will return a new slice with the slices of elements appended to the
// end.
//
// It is acceptable to provide zero arguments.
func (ss SliceType) Extend(slices ...SliceType) (ss2 SliceType) {
ss2 = ss
for _, slice := range slices {
ss2 = ss2.Append(slice...)
}
return ss2
}
`,
"Filter": `package functions
// Filter will return a new slice containing only the elements that return
// true from the condition. The returned slice may contain zero elements (nil).
//
// FilterNot works in the opposite way of Filter.
func (ss SliceType) Filter(condition func(ElementType) bool) (ss2 SliceType) {
for _, s := range ss {
if condition(s) {
ss2 = append(ss2, s)
}
}
return
}
`,
"FilterNot": `package functions
// FilterNot works the same as Filter, with a negated condition. That is, it will
// return a new slice only containing the elements that returned false from the
// condition. The returned slice may contain zero elements (nil).
func (ss SliceType) FilterNot(condition func(ElementType) bool) (ss2 SliceType) {
for _, s := range ss {
if !condition(s) {
ss2 = append(ss2, s)
}
}
return
}
`,
"FindFirstUsing": `package functions
// FindFirstUsing will return the index of the first element when the callback returns true or -1 if no element is found.
// It follows the same logic as the findIndex() function in Javascript.
//
// If the list is empty then -1 is always returned.
func (ss SliceType) FindFirstUsing(fn func(value ElementType) bool) int {
for idx, value := range ss {
if fn(value) {
return idx
}
}
return -1
}
`,
"First": `package functions
// First returns the first element, or zero. Also see FirstOr().
func (ss SliceType) First() ElementType {
return ss.FirstOr(ElementZeroValue)
}
`,
"FirstOr": `package functions
// FirstOr returns the first element or a default value if there are no
// elements.
func (ss SliceType) FirstOr(defaultValue ElementType) ElementType {
if len(ss) == 0 {
return defaultValue
}
return ss[0]
}
`,
"Float64s": `package functions
import (
"github.com/elliotchance/pie/pie"
"strconv"
)
// Float64s transforms each element to a float64.
func (ss SliceType) Float64s() pie.Float64s {
l := len(ss)
// Avoid the allocation.
if l == 0 {
return nil
}
result := make(pie.Float64s, l)
for i := 0; i < l; i++ {
mightBeString := ss[i]
result[i], _ = strconv.ParseFloat(mightBeString.String(), 64)
}
return result
}
`,
"Intersect": `package functions
// Intersect returns items that exist in all lists.
//
// It returns slice without any duplicates.
// If zero slice arguments are provided, then nil is returned.
func (ss SliceType) Intersect(slices ...SliceType) (ss2 SliceType) {
if slices == nil {
return nil
}
var uniqs = make([]map[ElementType]struct{}, len(slices))
for i := 0; i < len(slices); i++ {
m := make(map[ElementType]struct{})
for _, el := range slices[i] {
m[el] = struct{}{}
}
uniqs[i] = m
}
var containsInAll = false
for _, el := range ss.Unique() {
for _, u := range uniqs {
if _, exists := u[el]; !exists {
containsInAll = false
break
}
containsInAll = true
}
if containsInAll {
ss2 = append(ss2, el)
}
}
return
}
`,
"Ints": `package functions
import (
"github.com/elliotchance/pie/pie"
"strconv"
)
// Ints transforms each element to an integer.
func (ss SliceType) Ints() pie.Ints {
l := len(ss)
// Avoid the allocation.
if l == 0 {
return nil
}
result := make(pie.Ints, l)
for i := 0; i < l; i++ {
mightBeString := ss[i]
f, _ := strconv.ParseFloat(mightBeString.String(), 64)
result[i] = int(f)
}
return result
}
`,
"JSONBytes": `package functions
import (
"encoding/json"
)
// JSONBytes returns the JSON encoded array as bytes.
//
// One important thing to note is that it will treat a nil slice as an empty
// slice to ensure that the JSON value return is always an array.
func (ss SliceType) JSONBytes() []byte {
if ss == nil {
return []byte("[]")
}
// An error should not be possible.
data, _ := json.Marshal(ss)
return data
}
`,
"JSONBytesIndent": `package functions
import (
"encoding/json"
)
// JSONBytesIndent returns the JSON encoded array as bytes with indent applied.
//
// One important thing to note is that it will treat a nil slice as an empty
// slice to ensure that the JSON value return is always an array. See
// json.MarshalIndent for details.
func (ss SliceType) JSONBytesIndent(prefix, indent string) []byte {
if ss == nil {
return []byte("[]")
}
// An error should not be possible.
data, _ := json.MarshalIndent(ss, prefix, indent)
return data
}
`,
"JSONString": `package functions
import (
"encoding/json"
)
// JSONString returns the JSON encoded array as a string.
//
// One important thing to note is that it will treat a nil slice as an empty
// slice to ensure that the JSON value return is always an array.
func (ss SliceType) JSONString() string {
if ss == nil {
return "[]"
}
// An error should not be possible.
data, _ := json.Marshal(ss)
return string(data)
}
`,
"JSONStringIndent": `package functions
import (
"encoding/json"
)
// JSONStringIndent returns the JSON encoded array as a string with indent applied.
//
// One important thing to note is that it will treat a nil slice as an empty
// slice to ensure that the JSON value return is always an array. See
// json.MarshalIndent for details.
func (ss SliceType) JSONStringIndent(prefix, indent string) string {
if ss == nil {
return "[]"
}
// An error should not be possible.
data, _ := json.MarshalIndent(ss, prefix, indent)
return string(data)
}
`,
"Join": `package functions
// Join returns a string from joining each of the elements.
func (ss StringSliceType) Join(glue string) (s string) {
for i, element := range ss {
if i > 0 {
s += glue
}
s += string(element)
}
return s
}
`,
"Keys": `package functions
// Keys returns the keys in the map. All of the items will be unique.
//
// Due to Go's randomization of iterating maps the order is not deterministic.
func (m MapType) Keys() KeySliceType {
// Avoid allocation
l := len(m)
if l == 0 {
return nil
}
i := 0
keys := make(KeySliceType, len(m))
for key := range m {
keys[i] = key
i++
}
return keys
}
`,
"Last": `package functions
// Last returns the last element, or zero. Also see LastOr().
func (ss SliceType) Last() ElementType {
return ss.LastOr(ElementZeroValue)
}
`,
"LastOr": `package functions
// LastOr returns the last element or a default value if there are no elements.
func (ss SliceType) LastOr(defaultValue ElementType) ElementType {
if len(ss) == 0 {
return defaultValue
}
return ss[len(ss)-1]
}
`,
"Len": `package functions
// Len returns the number of elements.
func (ss SliceType) Len() int {
return len(ss)
}
`,
"Map": `package functions
// Map will return a new slice where each element has been mapped (transformed).
// The number of elements returned will always be the same as the input.
//
// Be careful when using this with slices of pointers. If you modify the input
// value it will affect the original slice. Be sure to return a new allocated
// object or deep copy the existing one.
func (ss SliceType) Map(fn func(ElementType) ElementType) (ss2 SliceType) {
if ss == nil {
return nil
}
ss2 = make([]ElementType, len(ss))
for i, s := range ss {
ss2[i] = fn(s)
}
return
}
`,
"Max": `package functions
// Max is the maximum value, or zero.
func (ss SliceType) Max() (max ElementType) {
if len(ss) == 0 {
return
}
max = ss[0]
for _, s := range ss {
if s > max {
max = s
}
}
return
}
`,
"Median": `package functions
// Median returns the value separating the higher half from the lower half of a
// data sample.
//
// Zero is returned if there are no elements in the slice.
//
// If the number of elements is even, then the ElementType mean of the two "median values"
// is returned.
func (ss SliceType) Median() ElementType {
n := len(ss)
if n == 0 {
return ElementZeroValue
}
if n == 1 {
return ss[0]
}
// This implementation aims at linear time O(n) on average.
// It uses the same idea as QuickSort, but makes only 1 recursive
// call instead of 2. See also Quickselect.
work := make(SliceType, len(ss))
copy(work, ss)
limit1, limit2 := n/2, n/2+1
if n%2 == 0 {
limit1, limit2 = n/2-1, n/2+1
}
var rec func(a, b int)
rec = func(a, b int) {
if b-a <= 1 {
return
}
ipivot := (a + b) / 2
pivot := work[ipivot]
work[a], work[ipivot] = work[ipivot], work[a]
j := a
k := b
for j+1 < k {
if work[j+1] < pivot {
work[j+1], work[j] = work[j], work[j+1]
j++
} else {
work[j+1], work[k-1] = work[k-1], work[j+1]
k--
}
}
// 1 or 0 recursive calls
if j > limit1 {
rec(a, j)
}
if j+1 < limit2 {
rec(j+1, b)
}
}
rec(0, len(work))
if n%2 == 1 {
return work[n/2]
} else {
return (work[n/2-1] + work[n/2]) / 2
}
}
`,
"Min": `package functions
// Min is the minimum value, or zero.
func (ss SliceType) Min() (min ElementType) {
if len(ss) == 0 {
return
}
min = ss[0]
for _, s := range ss {
if s < min {
min = s
}
}
return
}
`,
"Mode": `package functions
// Mode returns a new slice containing the most frequently occuring values.
//
// The number of items returned may be the same as the input or less. It will
// never return zero items unless the input slice has zero items.
func (ss SliceType) Mode() SliceType {
if len(ss) == 0 {
return nil
}
values := make(map[ElementType]int, 0)
for _, s := range ss {
values[s]++
}
var maxFrequency int
for _, v := range values {
if v > maxFrequency {
maxFrequency = v
}
}
var maxValues SliceType
for k, v := range values {
if v == maxFrequency {
maxValues = append(maxValues, k)
}
}
return maxValues
}
`,
"Product": `package functions
// Product is the product of all of the elements.
func (ss SliceType) Product() (product ElementType) {
if len(ss) == 0 {
return
}
product = ss[0]
for _, s := range ss[1:] {
product *= s
}
return
}
`,
"Random": `package functions
import (
"math/rand"
)
// Random returns a random element by your rand.Source, or zero
func (ss SliceType) Random(source rand.Source) ElementType {
n := len(ss)
// Avoid the extra allocation.
if n < 1 {
return ElementZeroValue
}
if n < 2 {
return ss[0]
}
rnd := rand.New(source)
i := rnd.Intn(n)
return ss[i]
}
`,
"Reduce": `package functions
// Reduce continually applies the provided function
// over the slice. Reducing the elements to a single value.
//
// Returns a zero value of ElementType if there are no elements in the slice. It will panic if the reducer is nil and the slice has more than one element (required to invoke reduce).
// Otherwise returns result of applying reducer from left to right.
func (ss SliceType) Reduce(reducer func(ElementType, ElementType) ElementType) (el ElementType) {
if len(ss) == 0 {
return
}
el = ss[0]
for _, s := range ss[1:] {
el = reducer(el, s)
}
return
}
`,
"Reverse": `package functions
// Reverse returns a new copy of the slice with the elements ordered in reverse.
// This is useful when combined with Sort to get a descending sort order:
//
// ss.Sort().Reverse()
//
func (ss SliceType) Reverse() SliceType {
// Avoid the allocation. If there is one element or less it is already
// reversed.
if len(ss) < 2 {
return ss
}
sorted := make([]ElementType, len(ss))
for i := 0; i < len(ss); i++ {
sorted[i] = ss[len(ss)-i-1]
}
return sorted
}
`,
"Send": `package functions
import (
"context"
)
// Send sends elements to channel
// in normal act it sends all elements but if func canceled it can be less
//
// it locks execution of gorutine
// it doesn't close channel after work
// returns sended elements if len(this) != len(old) considered func was canceled
func (ss SliceType) Send(ctx context.Context, ch chan<- ElementType) SliceType {
for i, s := range ss {
select {
case <-ctx.Done():
return ss[:i]
default:
ch <- s
}
}
return ss
}
`,
"Sequence": `package functions
// Sequence generates all numbers in range or returns nil if params invalid
//
// There are 3 variations to generate:
// 1. [0, n).
// 2. [min, max).
// 3. [min, max) with step.
//
// if len(params) == 1 considered that will be returned slice between 0 and n,
// where n is the first param, [0, n).
// if len(params) == 2 considered that will be returned slice between min and max,
// where min is the first param, max is the second, [min, max).
// if len(params) > 2 considered that will be returned slice between min and max with step,
// where min is the first param, max is the second, step is the third one, [min, max) with step,
// others params will be ignored
func (ss SliceType) Sequence(params ...int) SliceType {
var creator = func(i int) ElementType {
return ElementType(i)
}
return ss.SequenceUsing(creator, params...)
}
`,
"SequenceUsing": `package functions
import "github.com/elliotchance/pie/pie/util"
// SequenceUsing generates slice in range using creator function
//
// There are 3 variations to generate:
// 1. [0, n).
// 2. [min, max).
// 3. [min, max) with step.
//
// if len(params) == 1 considered that will be returned slice between 0 and n,
// where n is the first param, [0, n).
// if len(params) == 2 considered that will be returned slice between min and max,
// where min is the first param, max is the second, [min, max).
// if len(params) > 2 considered that will be returned slice between min and max with step,
// where min is the first param, max is the second, step is the third one, [min, max) with step,
// others params will be ignored
func (ss SliceType) SequenceUsing(creator func(int) ElementType, params ...int) SliceType {
var seq = func(min, max, step int) (seq SliceType) {
lenght := int(util.Round(float64(max-min) / float64(step)))
if lenght < 1 {
return
}
seq = make(SliceType, lenght)
for i := 0; i < lenght; min += step {
seq[i] = creator(min)
i++
}
return seq
}
if len(params) > 2 {
return seq(params[0], params[1], params[2])
} else if len(params) == 2 {
return seq(params[0], params[1], 1)
} else if len(params) == 1 {
return seq(0, params[0], 1)
} else {
return nil
}
}
`,
"Shuffle": `package functions
import (
"github.com/elliotchance/pie/pie/util"
"math/rand"
)
// Shuffle returns shuffled slice by your rand.Source
func (ss SliceType) Shuffle(source rand.Source) SliceType {
n := len(ss)
// Avoid the extra allocation.
if n < 2 {
return ss
}
// go 1.10+ provides rnd.Shuffle. However, to support older versions we copy
// the algorithm directly from the go source: src/math/rand/rand.go below,
// with some adjustments:
shuffled := make([]ElementType, n)
copy(shuffled, ss)
rnd := rand.New(source)
util.Shuffle(rnd, n, func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
return shuffled
}
`,
"Sort": `package functions
import (
"sort"
)
// Sort works similar to sort.SliceType(). However, unlike sort.SliceType the
// slice returned will be reallocated as to not modify the input slice.
//
// See Reverse() and AreSorted().
func (ss SliceType) Sort() SliceType {
// Avoid the allocation. If there is one element or less it is already
// sorted.
if len(ss) < 2 {
return ss
}
sorted := make(SliceType, len(ss))
copy(sorted, ss)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
return sorted
}
`,
"SortStableUsing": `package functions
import (
"sort"
)
// SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the
// slice returned will be reallocated as to not modify the input slice.
func (ss SliceType) SortStableUsing(less func(a, b ElementType) bool) SliceType {
// Avoid the allocation. If there is one element or less it is already
// sorted.
if len(ss) < 2 {
return ss
}
sorted := make(SliceType, len(ss))
copy(sorted, ss)
sort.SliceStable(sorted, func(i, j int) bool {
return less(sorted[i], sorted[j])
})
return sorted
}
`,
"SortUsing": `package functions
import (
"sort"
)
// SortUsing works similar to sort.Slice. However, unlike sort.Slice the
// slice returned will be reallocated as to not modify the input slice.
func (ss SliceType) SortUsing(less func(a, b ElementType) bool) SliceType {
// Avoid the allocation. If there is one element or less it is already
// sorted.
if len(ss) < 2 {
return ss
}
sorted := make(SliceType, len(ss))
copy(sorted, ss)
sort.Slice(sorted, func(i, j int) bool {
return less(sorted[i], sorted[j])
})
return sorted
}
`,
"Strings": `package functions
import (
"github.com/elliotchance/pie/pie"
)
// Strings transforms each element to a string.
//
// If the element type implements fmt.Stringer it will be used. Otherwise it
// will fallback to the result of:
//