@@ -224,9 +224,7 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
224224 }
225225
226226 // Copy chunk in a new slice, to prevent memory leak and free memory from initial collection.
227- newSlice := make (Slice , last - i * size )
228- copy (newSlice , collection [i * size :last ])
229- result = append (result , newSlice )
227+ result = append (result , clone (collection [i * size :last ]))
230228 }
231229
232230 return result
@@ -312,9 +310,7 @@ func Sliding[T any, Slice ~[]T](collection Slice, size, step int) []Slice {
312310 result := make ([]Slice , 0 , n / step + 1 )
313311
314312 for i := 0 ; i <= n ; i += step {
315- window := make (Slice , size )
316- copy (window , collection [i :i + size ])
317- result = append (result , window )
313+ result = append (result , clone (collection [i :i + size ]))
318314 }
319315
320316 return result
@@ -517,12 +513,10 @@ func Drop[T any, Slice ~[]T](collection Slice, n int) Slice {
517513 }
518514
519515 if len (collection ) <= n {
520- return make ( Slice , 0 )
516+ return Slice {}
521517 }
522518
523- result := make (Slice , 0 , len (collection )- n )
524-
525- return append (result , collection [n :]... )
519+ return clone (collection [n :])
526520}
527521
528522// DropRight drops n elements from the end of a slice.
@@ -536,8 +530,7 @@ func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice {
536530 return Slice {}
537531 }
538532
539- result := make (Slice , 0 , len (collection )- n )
540- return append (result , collection [:len (collection )- n ]... )
533+ return clone (collection [:len (collection )- n ])
541534}
542535
543536// DropWhile drops elements from the beginning of a slice while the predicate returns true.
@@ -550,8 +543,7 @@ func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool)
550543 }
551544 }
552545
553- result := make (Slice , 0 , len (collection )- i )
554- return append (result , collection [i :]... )
546+ return clone (collection [i :])
555547}
556548
557549// DropRightWhile drops elements from the end of a slice while the predicate returns true.
@@ -564,8 +556,7 @@ func DropRightWhile[T any, Slice ~[]T](collection Slice, predicate func(item T)
564556 }
565557 }
566558
567- result := make (Slice , 0 , i + 1 )
568- return append (result , collection [:i + 1 ]... )
559+ return clone (collection [:i + 1 ])
569560}
570561
571562// Take takes the first n elements from a slice.
@@ -574,24 +565,11 @@ func Take[T any, Slice ~[]T](collection Slice, n int) Slice {
574565 panic ("lo.Take: n must not be negative" )
575566 }
576567
577- if n == 0 {
578- return make (Slice , 0 )
579- }
580-
581- size := len (collection )
582- if size == 0 {
583- return make (Slice , 0 )
568+ if len (collection ) > n {
569+ collection = collection [:n ]
584570 }
585571
586- if n >= size {
587- result := make (Slice , size )
588- copy (result , collection )
589- return result
590- }
591-
592- result := make (Slice , n )
593- copy (result , collection )
594- return result
572+ return clone (collection )
595573}
596574
597575// TakeWhile takes elements from the beginning of a slice while the predicate returns true.
@@ -603,9 +581,7 @@ func TakeWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool)
603581 }
604582 }
605583
606- result := make (Slice , i )
607- copy (result , collection [:i ])
608- return result
584+ return clone (collection [:i ])
609585}
610586
611587// DropByIndex drops elements from a slice by the index.
@@ -614,7 +590,7 @@ func TakeWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool)
614590func DropByIndex [T any , Slice ~ []T ](collection Slice , indexes ... int ) Slice {
615591 initialSize := len (collection )
616592 if initialSize == 0 {
617- return make ( Slice , 0 )
593+ return Slice {}
618594 }
619595
620596 for i := range indexes {
@@ -648,7 +624,7 @@ func TakeFilter[T any, Slice ~[]T](collection Slice, n int, predicate func(item
648624 }
649625
650626 if n == 0 {
651- return make ( Slice , 0 )
627+ return Slice {}
652628 }
653629
654630 result := make (Slice , 0 , n )
@@ -822,11 +798,10 @@ func Slice[T any, Slice ~[]T](collection Slice, start, end int) Slice {
822798// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
823799// Play: https://go.dev/play/p/XfPzmf9gql6
824800func Replace [T comparable , Slice ~ []T ](collection Slice , old , nEw T , n int ) Slice {
825- result := make (Slice , len (collection ))
826- copy (result , collection )
801+ result := clone (collection )
827802
828803 for i := range result {
829- if result [ i ] == old && n != 0 {
804+ if n != 0 && result [ i ] == old {
830805 result [i ] = nEw
831806 n --
832807 }
@@ -850,6 +825,12 @@ func Clone[T any, Slice ~[]T](collection Slice) Slice {
850825 if collection == nil {
851826 return nil
852827 }
828+
829+ return clone (collection )
830+ }
831+
832+ // clone returns a shallow copy of the collection without preserving nilness.
833+ func clone [T any , Slice ~ []T ](collection Slice ) Slice {
853834 // Avoid s[:0:0] as it leads to unwanted liveness when cloning a
854835 // zero-length slice of a large array; see https://go.dev/issue/68488.
855836 return append (Slice {}, collection ... )
@@ -934,7 +915,7 @@ func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice {
934915// Play: https://go.dev/play/p/GiL3qhpIP3f
935916func Cut [T comparable , Slice ~ []T ](collection , separator Slice ) (before , after Slice , found bool ) {
936917 if len (separator ) == 0 {
937- return make ( Slice , 0 ) , collection , true
918+ return Slice {} , collection , true
938919 }
939920
940921 for i := 0 ; i + len (separator ) <= len (collection ); i ++ {
@@ -950,7 +931,7 @@ func Cut[T comparable, Slice ~[]T](collection, separator Slice) (before, after S
950931 }
951932 }
952933
953- return collection , make ( Slice , 0 ) , false
934+ return collection , Slice {} , false
954935}
955936
956937// CutPrefix returns collection without the provided leading prefix []T
@@ -1020,8 +1001,7 @@ func Trim[T comparable, Slice ~[]T](collection, cutset Slice) Slice {
10201001 }
10211002 }
10221003
1023- result := make (Slice , 0 , j + 1 - i )
1024- return append (result , collection [i :j + 1 ]... )
1004+ return clone (collection [i : j + 1 ])
10251005}
10261006
10271007// TrimLeft removes all the leading cutset from the collection.
@@ -1042,12 +1022,11 @@ func TrimPrefix[T comparable, Slice ~[]T](collection, prefix Slice) Slice {
10421022 return collection
10431023 }
10441024
1045- for {
1046- if ! HasPrefix (collection , prefix ) {
1047- return collection
1048- }
1025+ for HasPrefix (collection , prefix ) {
10491026 collection = collection [len (prefix ):]
10501027 }
1028+
1029+ return collection
10511030}
10521031
10531032// TrimRight removes all the trailing cutset from the collection.
@@ -1068,10 +1047,9 @@ func TrimSuffix[T comparable, Slice ~[]T](collection, suffix Slice) Slice {
10681047 return collection
10691048 }
10701049
1071- for {
1072- if ! HasSuffix (collection , suffix ) {
1073- return collection
1074- }
1050+ for HasSuffix (collection , suffix ) {
10751051 collection = collection [:len (collection )- len (suffix )]
10761052 }
1053+
1054+ return collection
10771055}
0 commit comments