forked from phR0ze/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_inter.go
885 lines (785 loc) · 24.2 KB
/
slice_inter.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
package n
import (
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
)
// InterSlice implements the Slice interface providing a generic way to work with slice types
// including convenience methods on par with rapid development languages. This type incurs
// some reflection overhead characteristics but not all and differs in one important way from
// the RefSlice type. The given slice will be converted to a slice of types interface and
// left that way while RefSlice keeps the internal types typed as they were originally.
type InterSlice []interface{}
// NewInterSlice uses reflection to encapsulate the given Go slice type inside a new *InterSlice.
// Expects a Go slice type to be provided and will create an empty *InterSlice if nothing valid
// is given.
func NewInterSlice(slice interface{}) (new *InterSlice) {
return ToInterSlice(slice)
}
// NewInterSliceV creates a new *InterSlice from the given variadic elements. Always returns
// at least a reference to an empty InterSlice.
func NewInterSliceV(elems ...interface{}) (new *InterSlice) {
if len(elems) == 0 {
new = &InterSlice{}
} else {
s := InterSlice(elems)
new = &s
}
return
}
// A is an alias to String for brevity
func (p *InterSlice) A() string {
return p.String()
}
// All tests if this Slice is not empty or optionally if it contains
// all of the given variadic elements. Incompatible types will return false.
func (p *InterSlice) All(elems ...interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Not looking for anything
if len(elems) == 0 {
return true
}
// Looking for something specific returns false if incompatible type
return p.AllS(elems)
}
// AllS tests if this Slice contains all of the given Slice's elements.
// Incompatible types will return false.
// Supports InterSlice, *InterSlice, Slice and Go slice types
func (p *InterSlice) AllS(slice interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
elems := ToInterSlice(slice)
for i := range *elems {
found := false
for j := range *p {
if (*p)[j] == (*elems)[i] {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// Any tests if this Slice is not empty or optionally if it contains
// any of the given variadic elements. Incompatible types will return false.
func (p *InterSlice) Any(elems ...interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Not looking for anything
if len(elems) == 0 {
return true
}
// Looking for something specific returns false if incompatible type
return p.AnyS(elems)
}
// AnyS tests if this Slice contains any of the given Slice's elements.
// Incompatible types will return false.
// Supports InterSlice, *InterSlice, Slice and Go slice types
func (p *InterSlice) AnyS(slice interface{}) bool {
if p == nil || len(*p) == 0 {
return false
}
elems := ToInterSlice(slice)
for i := range *elems {
for j := range *p {
if (*p)[j] == (*elems)[i] {
return true
}
}
}
return false
}
// AnyW tests if this Slice contains any that match the lambda selector.
func (p *InterSlice) AnyW(sel func(O) bool) bool {
return p.CountW(sel) != 0
}
// Append an element to the end of this Slice and returns a reference to this Slice.
func (p *InterSlice) Append(elem interface{}) ISlice {
if p == nil {
p = NewInterSliceV()
}
*p = append(*p, elem)
return p
}
// AppendV appends the variadic elements to the end of this Slice and returns a reference to this Slice.
func (p *InterSlice) AppendV(elems ...interface{}) ISlice {
if p == nil {
p = NewInterSliceV()
}
*p = append(*p, elems...)
return p
}
// At returns the element at the given index location. Allows for negative notation.
func (p *InterSlice) At(i int) (elem *Object) {
elem = &Object{}
if p == nil {
return
}
if i = absIndex(len(*p), i); i == -1 {
return
}
elem.o = (*p)[i]
return
}
// Clear modifies this Slice to clear out all elements and returns a reference to this Slice.
func (p *InterSlice) Clear() ISlice {
if p == nil {
p = NewInterSliceV()
} else {
p.Drop()
}
return p
}
// Concat returns a new Slice by appending the given Slice to this Slice using variadic expansion.
// Supports InterSlice, *InterSlice, []int or *[]int
func (p *InterSlice) Concat(slice interface{}) (new ISlice) {
return p.Copy().ConcatM(slice)
}
// ConcatM modifies this Slice by appending the given Slice using variadic expansion and returns a reference to this Slice.
// Supports InterSlice, *InterSlice, Slice and Go slice types
func (p *InterSlice) ConcatM(slice interface{}) ISlice {
if p == nil {
p = NewInterSliceV()
}
elems := ToInterSlice(slice)
*p = append(*p, *elems...)
return p
}
// Copy returns a new Slice with the indicated range of elements copied from this Slice.
// Expects nothing, in which case everything is copied, or two indices i and j, in which
// case positive and negative notation is supported and uses an inclusive behavior such
// that Slice(0, -1) includes index -1 as opposed to Go's exclusive behavior. Out of
// bounds indices will be moved within bounds.
//
// An empty Slice is returned if indicies are mutually exclusive or nothing can be returned.
func (p *InterSlice) Copy(indices ...int) (new ISlice) {
if p == nil || len(*p) == 0 {
return NewInterSliceV()
}
// Handle index manipulation
i, j, err := absIndices(len(*p), indices...)
if err != nil {
return NewInterSliceV()
}
// Copy elements over to new Slice
x := make([]interface{}, j-i, j-i)
copy(x, (*p)[i:j])
return NewInterSlice(x)
}
// Count the number of elements in this Slice equal to the given element.
func (p *InterSlice) Count(elem interface{}) (cnt int) {
cnt = p.CountW(func(x O) bool { return ExB(x == elem) })
return
}
// CountW counts the number of elements in this Slice that match the lambda selector.
func (p *InterSlice) CountW(sel func(O) bool) (cnt int) {
if p == nil || len(*p) == 0 {
return
}
for i := range *p {
if sel((*p)[i]) {
cnt++
}
}
return
}
// Drop modifies this Slice to delete the indicated range of elements and returns a referece to this Slice.
// Expects nothing, in which case everything is dropped, or two indices i and j, in which case positive and
// negative notation is supported and uses an inclusive behavior such that DropAt(0, -1) includes index -1
// as opposed to Go's exclusive behavior. Out of bounds indices will be moved within bounds.
func (p *InterSlice) Drop(indices ...int) ISlice {
if p == nil || len(*p) == 0 {
return p
}
// Handle index manipulation
i, j, err := absIndices(len(*p), indices...)
if err != nil {
return p
}
// Execute
n := j - i
if i+n < len(*p) {
*p = append((*p)[:i], (*p)[i+n:]...)
} else {
*p = (*p)[:i]
}
return p
}
// DropAt modifies this Slice to delete the element at the given index location. Allows for negative notation.
// Returns a reference to this Slice.
func (p *InterSlice) DropAt(i int) ISlice {
return p.Drop(i, i)
}
// DropFirst modifies this Slice to delete the first element and returns a reference to this Slice.
func (p *InterSlice) DropFirst() ISlice {
return p.Drop(0, 0)
}
// DropFirstN modifies this Slice to delete the first n elements and returns a reference to this Slice.
func (p *InterSlice) DropFirstN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(0, abs(n)-1)
}
// DropLast modifies this Slice to delete the last element and returns a reference to this Slice.
func (p *InterSlice) DropLast() ISlice {
return p.Drop(-1, -1)
}
// DropLastN modifies thi Slice to delete the last n elements and returns a reference to this Slice.
func (p *InterSlice) DropLastN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(absNeg(n), -1)
}
// DropW modifies this Slice to delete the elements that match the lambda selector and returns a reference to this Slice.
// The slice is updated instantly when lambda expression is evaluated not after DropW completes.
func (p *InterSlice) DropW(sel func(O) bool) ISlice {
if p == nil || len(*p) == 0 {
return p
}
l := len(*p)
for i := 0; i < l; i++ {
if sel((*p)[i]) {
p.DropAt(i)
l--
i--
}
}
return p
}
// Each calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *InterSlice) Each(action func(O)) ISlice {
if p == nil {
return p
}
for i := range *p {
action((*p)[i])
}
return p
}
// EachE calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *InterSlice) EachE(action func(O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := range *p {
if err = action((*p)[i]); err != nil {
return p, err
}
}
return p, err
}
// EachI calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Returns a reference to this Slice
func (p *InterSlice) EachI(action func(int, O)) ISlice {
if p == nil {
return p
}
for i := range *p {
action(i, (*p)[i])
}
return p
}
// EachIE calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *InterSlice) EachIE(action func(int, O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := range *p {
if err = action(i, (*p)[i]); err != nil {
return p, err
}
}
return p, err
}
// EachR calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *InterSlice) EachR(action func(O)) ISlice {
if p == nil {
return p
}
for i := len(*p) - 1; i >= 0; i-- {
action((*p)[i])
}
return p
}
// EachRE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *InterSlice) EachRE(action func(O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := len(*p) - 1; i >= 0; i-- {
if err = action((*p)[i]); err != nil {
return p, err
}
}
return p, err
}
// EachRI calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *InterSlice) EachRI(action func(int, O)) ISlice {
if p == nil {
return p
}
for i := len(*p) - 1; i >= 0; i-- {
action(i, (*p)[i])
}
return p
}
// EachRIE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *InterSlice) EachRIE(action func(int, O) error) (ISlice, error) {
var err error
if p == nil {
return p, err
}
for i := len(*p) - 1; i >= 0; i-- {
if err = action(i, (*p)[i]); err != nil {
return p, err
}
}
return p, err
}
// Empty tests if this Slice is empty.
func (p *InterSlice) Empty() bool {
if p == nil || len(*p) == 0 {
return true
}
return false
}
// First returns the first element in this Slice as Object.
// Object.Nil() == true will be returned when there are no elements in the slice.
func (p *InterSlice) First() (elem *Object) {
return p.At(0)
}
// FirstN returns the first n elements in this slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *InterSlice) FirstN(n int) ISlice {
if n == 0 {
return NewInterSliceV()
}
return p.Slice(0, abs(n)-1)
}
// G returns the underlying Go type as is
func (p *InterSlice) G() []interface{} {
if p == nil {
return []interface{}{}
}
return []interface{}(*p)
}
// Index returns the index of the first element in this Slice where element == elem
// Returns a -1 if the element was not not found.
func (p *InterSlice) Index(elem interface{}) (loc int) {
loc = -1
if p == nil || len(*p) == 0 {
return
}
for i := range *p {
if (*p)[i] == elem {
return i
}
}
return
}
// Insert modifies this Slice to insert the given element before the element with the given index.
// Negative indices count backwards from the end of the slice, where -1 is the last element. If a
// negative index is used, the given element will be inserted after that element, so using an index
// of -1 will insert the element at the end of the slice. If a Slice is given all elements will be
// inserted starting from the beging until the end. Slice is returned for chaining. Invalid
// index locations will not change the slice.
func (p *InterSlice) Insert(i int, obj interface{}) ISlice {
if p == nil || len(*p) == 0 {
return p.ConcatM(obj)
}
// Insert the item before j if pos and after j if neg
j := i
if j = absIndex(len(*p), j); j == -1 {
return p
}
if i < 0 {
j++
}
elems := ToInterSlice(obj)
if j == 0 {
*p = append(*elems, *p...)
} else if j < len(*p) {
*p = append(*p, *elems...) // ensures enough space exists
copy((*p)[j+len(*elems):], (*p)[j:]) // shifts right elements drop added
copy((*p)[j:], *elems) // set new in locations vacated
} else {
*p = append(*p, *elems...)
}
return p
}
// InterSlice returns true if the underlying implementation is a RefSlice
func (p *InterSlice) InterSlice() bool {
return true
}
// Join converts each element into a string then joins them together using the given separator or comma by default.
func (p *InterSlice) Join(separator ...string) (str *Object) {
if p == nil || len(*p) == 0 {
str = &Object{""}
return
}
sep := ","
if len(separator) > 0 {
sep = separator[0]
}
var builder strings.Builder
for i := range *p {
builder.WriteString(ToString((*p)[i]))
if i+1 < len(*p) {
builder.WriteString(sep)
}
}
str = &Object{builder.String()}
return
}
// Last returns the last element in this Slice as an Object.
// Object.Nil() == true will be returned if there are no elements in the slice.
func (p *InterSlice) Last() (elem *Object) {
return p.At(-1)
}
// LastN returns the last n elements in this Slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *InterSlice) LastN(n int) ISlice {
if n == 0 {
return NewInterSliceV()
}
return p.Slice(absNeg(n), -1)
}
// Len returns the number of elements in this Slice
func (p *InterSlice) Len() int {
if p == nil {
return 0
}
return len(*p)
}
// Less returns true if the element indexed by i is less than the element indexed by j.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *InterSlice) Less(i, j int) bool {
l := p.Len()
if p.Nil() || l < 2 || i < 0 || j < 0 || i >= l || j >= l {
return false
}
// Handle supported types
slice := Slice(*p)
if !slice.RefSlice() {
return slice.Less(i, j)
}
panic(fmt.Sprintf("unsupported comparable type '%T'", *p))
}
// Map creates a new slice with the modified elements from the lambda.
func (p *InterSlice) Map(mod func(O) O) ISlice {
var slice ISlice
if p == nil || len(*p) == 0 {
return NewInterSliceV()
}
for i := range *p {
v := mod((*p)[i])
if slice == nil {
slice = Slice(v)
} else {
slice.Append(v)
}
}
return slice
}
// Nil tests if this Slice is nil
func (p *InterSlice) Nil() bool {
if p == nil {
return true
}
return false
}
// O returns the underlying data structure as is
func (p *InterSlice) O() interface{} {
if p.Nil() {
return []interface{}{}
}
return []interface{}(*p)
}
// Pair simply returns the first and second Slice elements as Objects
func (p *InterSlice) Pair() (first, second *Object) {
first, second = &Object{}, &Object{}
if p == nil {
return
}
if len(*p) > 0 {
first = p.At(0)
}
if len(*p) > 1 {
second = p.At(1)
}
return
}
// Pop modifies this Slice to remove the last element and returns the removed element as an Object.
func (p *InterSlice) Pop() (elem *Object) {
elem = p.Last()
p.DropLast()
return
}
// PopN modifies this Slice to remove the last n elements and returns the removed elements as a new Slice.
func (p *InterSlice) PopN(n int) (new ISlice) {
if n == 0 {
return NewInterSliceV()
}
new = p.Copy(absNeg(n), -1)
p.DropLastN(n)
return
}
// Prepend modifies this Slice to add the given element at the begining and returns a reference to this Slice.
func (p *InterSlice) Prepend(elem interface{}) ISlice {
return p.Insert(0, elem)
}
// RefSlice returns true if the underlying implementation is a RefSlice
func (p *InterSlice) RefSlice() bool {
return false
}
// Reverse returns a new Slice with the order of the elements reversed.
func (p *InterSlice) Reverse() (new ISlice) {
if p == nil || len(*p) < 2 {
return p.Copy()
}
return p.Copy().ReverseM()
}
// ReverseM modifies this Slice reversing the order of the elements and returns a reference to this Slice.
func (p *InterSlice) ReverseM() ISlice {
if p == nil || len(*p) == 0 {
return p
}
for i, j := 0, len(*p)-1; i < j; i, j = i+1, j-1 {
p.Swap(i, j)
}
return p
}
// S is an alias to ToStringSlice
func (p *InterSlice) S() (slice *StringSlice) {
return ToStringSlice(p.O())
}
// Select creates a new slice with the elements that match the lambda selector.
func (p *InterSlice) Select(sel func(O) bool) (new ISlice) {
slice := NewInterSliceV()
if p == nil || len(*p) == 0 {
return slice
}
for i := range *p {
if sel((*p)[i]) {
*slice = append(*slice, (*p)[i])
}
}
return slice
}
// Set the element at the given index location to the given element. Allows for negative notation.
// Returns a reference to this Slice and swallows any errors.
func (p *InterSlice) Set(i int, elem interface{}) ISlice {
slice, _ := p.SetE(i, elem)
return slice
}
// SetE the element at the given index location to the given element. Allows for negative notation.
// Returns a referenc to this Slice and an error if out of bounds or elem is the wrong type.
func (p *InterSlice) SetE(i int, elems interface{}) (ISlice, error) {
var err error
if p == nil {
return p, err
}
if i = absIndex(len(*p), i); i == -1 {
err = errors.Errorf("slice assignment is out of bounds")
return p, err
}
// Account for length of elems
x := ToInterSlice(elems)
if len(*x) > 0 {
copy((*p)[i:], *x)
}
return p, err
}
// Shift modifies this Slice to remove the first element and returns the removed element as an Object.
func (p *InterSlice) Shift() (elem *Object) {
elem = p.First()
p.DropFirst()
return
}
// ShiftN modifies this Slice to remove the first n elements and returns the removed elements as a new Slice.
func (p *InterSlice) ShiftN(n int) (new ISlice) {
if n == 0 {
return NewInterSliceV()
}
new = p.Copy(0, abs(n)-1)
p.DropFirstN(n)
return
}
// Single reports true if there is only one element in this Slice.
func (p *InterSlice) Single() bool {
return p.Len() == 1
}
// Slice returns a range of elements from this Slice as a Slice reference to the original. Allows for negative notation.
// Expects nothing, in which case everything is included, or two indices i and j, in which case an inclusive behavior
// is used such that Slice(0, -1) includes index -1 as opposed to Go's exclusive behavior. Out of bounds indices will
// be moved within bounds.
//
// An empty Slice is returned if indicies are mutually exclusive or nothing can be returned.
//
// e.g. NewInterSliceV(1,2,3).Slice(0, -1) == [1,2,3] && NewInterSliceV(1,2,3).Slice(1,2) == [2,3]
func (p *InterSlice) Slice(indices ...int) ISlice {
if p == nil || len(*p) == 0 {
return NewInterSliceV()
}
// Handle index manipulation
i, j, err := absIndices(len(*p), indices...)
if err != nil {
return NewInterSliceV()
}
slice := InterSlice((*p)[i:j])
return &slice
}
// Sort returns a new Slice with sorted elements.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *InterSlice) Sort() (new ISlice) {
if p == nil || len(*p) < 2 {
return p.Copy()
}
return p.Copy().SortM()
}
// SortM modifies this Slice sorting the elements and returns a reference to this Slice.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *InterSlice) SortM() ISlice {
if p == nil || len(*p) < 2 {
return p
}
sort.Sort(p)
return p
}
// SortReverse returns a new Slice sorting the elements in reverse.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *InterSlice) SortReverse() (new ISlice) {
if p == nil || len(*p) < 2 {
return p.Copy()
}
return p.Copy().SortReverseM()
}
// SortReverseM modifies this Slice sorting the elements in reverse and returns a reference to this Slice.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *InterSlice) SortReverseM() ISlice {
if p == nil || len(*p) < 2 {
return p
}
sort.Sort(sort.Reverse(p))
return p
}
// Returns a string representation of this Slice, implements the Stringer interface
func (p *InterSlice) String() string {
var builder strings.Builder
builder.WriteString("[")
if p != nil {
for i := range *p {
builder.WriteString(ToString((*p)[i]))
if i+1 < len(*p) {
builder.WriteString(" ")
}
}
}
builder.WriteString("]")
return builder.String()
}
// Swap modifies this Slice swapping the indicated elements.
func (p *InterSlice) Swap(i, j int) {
if p == nil || len(*p) < 2 || i < 0 || j < 0 || i >= len(*p) || j >= len(*p) {
return
}
(*p)[i], (*p)[j] = (*p)[j], (*p)[i]
}
// Take modifies this Slice removing the indicated range of elements from this Slice and returning them as a new Slice.
// Expects nothing, in which case everything is taken, or two indices i and j, in which case positive and negative
// notation is supported and uses an inclusive behavior such that Take(0, -1) includes index -1 as opposed to Go's
// exclusive behavior. Out of bounds indices will be moved within bounds.
func (p *InterSlice) Take(indices ...int) (new ISlice) {
new = p.Copy(indices...)
p.Drop(indices...)
return
}
// TakeAt modifies this Slice removing the elemement at the given index location and returns the removed element as an Object.
// Allows for negative notation.
func (p *InterSlice) TakeAt(i int) (elem *Object) {
elem = p.At(i)
p.DropAt(i)
return
}
// TakeW modifies this Slice removing the elements that match the lambda selector and returns them as a new Slice.
func (p *InterSlice) TakeW(sel func(O) bool) (new ISlice) {
slice := NewInterSliceV()
if p == nil || len(*p) == 0 {
return slice
}
l := len(*p)
for i := 0; i < l; i++ {
if sel((*p)[i]) {
*slice = append(*slice, (*p)[i])
p.DropAt(i)
l--
i--
}
}
return slice
}
// ToInts converts the underlying slice into a []int
func (p *InterSlice) ToInts() (slice []int) {
return ToIntSlice(p.O()).G()
}
// ToIntSlice converts the underlying slice into a *IntSlice
func (p *InterSlice) ToIntSlice() (slice *IntSlice) {
return ToIntSlice(p.O())
}
// ToInterSlice converts the given slice to a generic []interface{} slice
func (p *InterSlice) ToInterSlice() (slice []interface{}) {
if p == nil {
return []interface{}{}
}
return []interface{}(*p)
}
// ToStringSlice converts the underlying slice into a *StringSlice
func (p *InterSlice) ToStringSlice() (slice *StringSlice) {
return ToStringSlice(p.O())
}
// ToStrs converts the underlying slice into a []string slice
func (p *InterSlice) ToStrs() (slice []string) {
return ToStrs(p.O())
}
// Union returns a new Slice by joining uniq elements from this Slice with uniq elements from the given Slice while preserving order.
// Supports InterSlice, *InterSlice, Slice and Go slice types
func (p *InterSlice) Union(slice interface{}) (new ISlice) {
return p.Copy().UnionM(slice)
}
// UnionM modifies this Slice by joining uniq elements from this Slice with uniq elements from the given Slice while preserving order.
// Supports InterSlice, *InterSlice, Slice and Go slice types
func (p *InterSlice) UnionM(slice interface{}) ISlice {
return p.ConcatM(slice).UniqM()
}
// Uniq returns a new Slice with all non uniq elements removed while preserving element order.
// Cost for this call vs the UniqM is roughly the same, this one is appending that one dropping.
func (p *InterSlice) Uniq() (new ISlice) {
panic("NOT IMPLEMENTED")
}
// UniqM modifies this Slice to remove all non uniq elements while preserving element order.
// Cost for this call vs the Uniq is roughly the same, this one is dropping that one appending.
func (p *InterSlice) UniqM() ISlice {
panic("NOT IMPLEMENTED")
}