From e8a24ab59e782cf2ee37b8744550db75a39b244a Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Thu, 2 Feb 2023 17:54:02 +0800 Subject: [PATCH] 1 --- pkg/{utilsv2/slice.go => utils/utils_v2.go} | 13 ++++++++++--- .../slice_test.go => utils/utils_v2_test.go} | 12 ++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) rename pkg/{utilsv2/slice.go => utils/utils_v2.go} (95%) rename pkg/{utilsv2/slice_test.go => utils/utils_v2_test.go} (88%) diff --git a/pkg/utilsv2/slice.go b/pkg/utils/utils_v2.go similarity index 95% rename from pkg/utilsv2/slice.go rename to pkg/utils/utils_v2.go index ab0d2e945..342638421 100644 --- a/pkg/utilsv2/slice.go +++ b/pkg/utils/utils_v2.go @@ -1,4 +1,4 @@ -package utilsv2 +package utils import ( "sort" @@ -26,8 +26,8 @@ func Distinct[T comparable](ts []T) []T { }) } -// DeleteAt delete slice element, support negative number to delete the penultimate -func DeleteAt[E any](es []E, index ...int) []E { +// Delete delete slice element, support negative number to delete the penultimate +func Delete[E any](es []E, index ...int) []E { switch len(index) { case 0: return es @@ -58,6 +58,13 @@ func DeleteAt[E any](es []E, index ...int) []E { } } +// DeleteAt delete slice element, support negative number to delete the penultimate +func DeleteAt[E any](es *[]E, index ...int) []E { + v := Delete(*es, index...) + *es = v + return v +} + // IndexAny get the index of the element func IndexAny[E any, K comparable](es []E, e E, fn func(e E) K) int { k := fn(e) diff --git a/pkg/utilsv2/slice_test.go b/pkg/utils/utils_v2_test.go similarity index 88% rename from pkg/utilsv2/slice_test.go rename to pkg/utils/utils_v2_test.go index fd18c497e..270cf4d69 100644 --- a/pkg/utilsv2/slice_test.go +++ b/pkg/utils/utils_v2_test.go @@ -1,4 +1,4 @@ -package utilsv2 +package utils import ( "fmt" @@ -12,9 +12,9 @@ func TestDistinct(t *testing.T) { func TestDeleteAt(t *testing.T) { arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - fmt.Println(DeleteAt(arr, 0, 1, -1, -2)) - fmt.Println(DeleteAt(arr)) - fmt.Println(DeleteAt(arr, 1)) + fmt.Println(Delete(arr, 0, 1, -1, -2)) + fmt.Println(Delete(arr)) + fmt.Println(Delete(arr, 1)) } func TestSliceToMap(t *testing.T) { @@ -72,8 +72,8 @@ func TestCompleteAny(t *testing.T) { }) } - list = DeleteAt(list, -1) - ids = DeleteAt(ids, -1) + DeleteAt(&list, -1) + DeleteAt(&ids, -1) ok := CompleteAny(ids, list, func(t Item) int { return t.ID