8000 fix(package): [slice] functions with inconsistent return behaviour (#… · duke-git/lancet@4c64a16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c64a16

Browse files
authored
fix(package): [slice] functions with inconsistent return behaviour (#326)
* Some functions modified * Fixes in all the functions of slice.go * Re-use of swap function internal.
1 parent be45a25 commit 4c64a16

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

slice/slice.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -690,11 +690,12 @@ func IntSlice(slice any) []int {
690690
// DeleteAt delete the element of slice at index.
691691
// Play: https://go.dev/play/p/800B1dPBYyd
692692
func DeleteAt[T any](slice []T, index int) []T {
693+
result := append([]T(nil), slice...)
694+
693695
if index < 0 || index >= len(slice) {
694-
return slice[:len(slice)-1]
696+
return result[:len(slice)-1]
695697
}
696698

697-
result := append([]T(nil), slice...)
698699
copy(result[index:], result[index+1:])
699700

700701
// Set the last element to zero value, clean up the memory.
@@ -734,7 +735,8 @@ func Drop[T any](slice []T, n int) []T {
734735
}
735736

736737
if n <= 0 {
737-
return slice
738+
result := make([]T, 0, size)
739+
return append(result, slice...)
738740
}
739741

740742
result := make([]T, 0, size-n)
@@ -752,7 +754,8 @@ func DropRight[T any](slice []T, n int) []T {
752754
}
753755

754756
if n <= 0 {
755-
return slice
757+
result := make([]T, 0, size)
758+
return append(result, slice...)
756759
}
757760

758761
result := make([]T, 0, size-n)
@@ -798,7 +801,9 @@ func InsertAt[T any](slice []T, index int, value any) []T {
798801
size := len(slice)
799802

800803
if index < 0 || index > size {
801-
return slice
804+
result := make([]T, size)
805+
copy(result, slice)
806+
return result
802807
}
803808

804809
switch v := value.(type) {
@@ -815,21 +820,21 @@ func InsertAt[T any](slice []T, index int, value any) []T {
815820
copy(result[index+len(v):], slice[index:])
816821
return result
817822
default:
818-
return slice
823+
result := make([]T, size)
824+
copy(result, slice)
825+
return result
819826
}
820827
}
821828

822829
// UpdateAt update the slice element at index.
823830
// Play: https://go.dev/play/p/f3mh2KloWVm
824831
func UpdateAt[T any](slice []T, index int, value T) []T {
825-
if index < 0 || index >= len(slice) {
826-
return< 10000 /span> slice
827-
}
828-
829832
result := make([]T, len(slice))
830833
copy(result, slice)
831834

832-
result[index] = value
835+
if index >= 0 && index < len(slice) {
836+
result[index] = value
837+
}
833838

834839
return result
835840
}
@@ -1019,7 +1024,9 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
10191024
return []T{}
10201025
}
10211026
if len(slices) == 1 {
1022-
return Unique(slices[0])
1027+
result := make([]T, len(slices[0]))
1028+
copy(result, slices[0])
1029+
return Unique(result)
10231030
}
10241031

10251032
result := make([]T, 0)
@@ -1040,14 +1047,16 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
10401047
}
10411048

10421049
// Reverse return slice of element order is reversed to the given slice.
1050+
// Reverse modifies the slice in place.
10431051
// Play: https://go.dev/play/p/8uI8f1lwNrQ
10441052
func Reverse[T any](slice []T) {
10451053
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
10461054
slice[i], slice[j] = slice[j], slice[i]
10471055
}
10481056
}
10491057

1050-
// ReverseCopy return a new slice of element order is reversed to the given slice.
1058+
// ReverseCopy return a new slice of element where the order is reversed to the given
1059+
// slice.
10511060
// Play: https://go.dev/play/p/c9arEaP7Cg-
10521061
func ReverseCopy[T any](slice []T) []T {
10531062
result := make([]T, len(slice))
@@ -1065,7 +1074,7 @@ func Shuffle[T any](slice []T) []T {
10651074
rand.Seed(time.Now().UnixNano())
10661075

10671076
rand.Shuffle(len(slice), func(i, j int) {
1068-
slice[i], slice[j] = slice[j], slice[i]
1077+
swap(slice, i, j)
10691078
})
10701079

10711080
return slice
@@ -1238,11 +1247,12 @@ func SortByField[T any](slice []T, field string, sortType ...string) error {
12381247
// Without creates a slice excluding all given items.
12391248
// Play: https://go.dev/play/p/bwhEXEypThg
12401249
func Without[T comparable](slice []T, items ...T) []T {
1250+
result := make([]T, 0, len(slice))
1251+
12411252
if len(items) == 0 || len(slice) == 0 {
1242-
return slice
1253+
return append(result, slice...)
12431254
}
12441255

1245-
result := make([]T, 0, len(slice))
12461256
for _, v := range slice {
12471257
if 3EBF !Contain(items, v) {
12481258
result = append(result, v)

0 commit comments

Comments
 (0)
0