diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index b630ba9..bef9a8c 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -35,6 +35,7 @@ import ( - [DifferenceBy](#DifferenceBy) - [DifferenceWith](#DifferenceWith) - [DeleteAt](#DeleteAt) +- [DeleteRange](#DeleteRange) - [Drop](#Drop) - [DropRight](#DropRight) - [DropWhile](#DropWhile) @@ -516,12 +517,12 @@ func main() { ### DeleteAt -
删除切片中指定开始索引到结束索引的元素
+删除切片中指定索引的元素(不修改原切片)。
函数签名: ```go -func DeleteAt[T any](slice []T, start int, end ...int) +func DeleteAt[T any](slice []T, index int) []T ``` 示例:[运行](https://go.dev/play/p/pJ-d6MUWcvK) @@ -533,18 +534,66 @@ import ( ) func main() { - result1 := slice.DeleteAt([]string{"a", "b", "c"}, -1) - result2 := slice.DeleteAt([]string{"a", "b", "c"}, 0) - result3 := slice.DeleteAt([]string{"a", "b", "c"}, 0, 2) + chars := []string{"a", "b", "c", "d", "e"} + + result1 := slice.DeleteAt(chars, 0) + result2 := slice.DeleteAt(chars, 4) + result3 := slice.DeleteAt(chars, 5) + result4 := slice.DeleteAt(chars, 6) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + fmt.Println(result4) // Output: - // [a b c] - // [b c] - // [c] + // [b c d e] + // [a b c d] + // [a b c d] + // [a b c d] + +} +``` + +### DeleteRange + +删除切片中指定索引范围的元素(不修改原切片)。
+ +函数签名: + +```go +func DeleteRange[T any](slice []T, start, end int) []T +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + chars := []string{"a", "b", "c", "d", "e"} + + result1 := DeleteRange(chars, 0, 0) + result2 := DeleteRange(chars, 0, 1) + result3 := DeleteRange(chars, 0, 3) + result4 := DeleteRange(chars, 0, 4) + result5 := DeleteRange(chars, 0, 5) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // [a b c d e] + // [b c d e] + // [d e] + // [e] + // [] } ``` @@ -2475,18 +2524,18 @@ import ( func main() { nums := []int{1, 2, 3, 4, 5} - result1 := slice.Partition(nums) - result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) - result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) + result1 := slice.Partition(nums) + result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) + result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - // Output: - // [[1 2 3 4 5]] - // [[2 4] [1 3 5]] - // [[1 2] [3 4] [5]] + // Output: + // [[1 2 3 4 5]] + // [[2 4] [1 3 5]] + // [[1 2] [3 4] [5]] } ``` @@ -2510,13 +2559,13 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} + nums := []int{1, 2, 3, 4, 5} - val, idx := slice.Random(nums) - if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { - fmt.Println("okk") - } - // Output: - // okk + val, idx := slice.Random(nums) + if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { + fmt.Println("okk") + } + // Output: + // okk } ``` \ No newline at end of file diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index a51822c..b580ca9 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -35,6 +35,7 @@ import ( - [DifferenceBy](#DifferenceBy) - [DifferenceWith](#DifferenceWith) - [DeleteAt](#DeleteAt) +- [DeleteRange](#DeleteRange) - [Drop](#Drop) - [DropRight](#DropRight) - [DropWhile](#DropWhile) @@ -515,12 +516,12 @@ func main() { ### DeleteAt -Delete the element of slice from start index to end index - 1.
+Delete delete the element of slice at index.
Signature: ```go -func DeleteAt[T any](slice []T, start int, end ...int) +func DeleteAt[T any](slice []T, index int) ``` Example:[Run](https://go.dev/play/p/pJ-d6MUWcvK) @@ -532,18 +533,66 @@ import ( ) func main() { - result1 := slice.DeleteAt([]string{"a", "b", "c"}, -1) - result2 := slice.DeleteAt([]string{"a", "b", "c"}, 0) - result3 := slice.DeleteAt([]string{"a", "b", "c"}, 0, 2) + chars := []string{"a", "b", "c", "d", "e"} + + result1 := slice.DeleteAt(chars, 0) + result2 := slice.DeleteAt(chars, 4) + result3 := slice.DeleteAt(chars, 5) + result4 := slice.DeleteAt(chars, 6) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + fmt.Println(result4) // Output: - // [a b c] - // [b c] - // [c] + // [b c d e] + // [a b c d] + // [a b c d] + // [a b c d] +} +``` + +### DeleteRange + +Delete the element of slice from start index to end index(exclude)
+ +Signature: + +```go +func DeleteRange[T any](slice []T, start, end int) []T +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + chars := []string{"a", "b", "c", "d", "e"} + + result1 := DeleteRange(chars, 0, 0) + result2 := DeleteRange(chars, 0, 1) + result3 := DeleteRange(chars, 0, 3) + result4 := DeleteRange(chars, 0, 4) + result5 := DeleteRange(chars, 0, 5) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // [a b c d e] + // [b c d e] + // [d e] + // [e] + // [] + } ``` @@ -2473,18 +2522,18 @@ import ( func main() { nums := []int{1, 2, 3, 4, 5} - result1 := slice.Partition(nums) - result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) - result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) + result1 := slice.Partition(nums) + result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) + result3 := slice.Partition(nums, func(n int) bool { return n == 1 || n == 2 }, func(n int) bool { return n == 2 || n == 3 || n == 4 }) - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - // Output: - // [[1 2 3 4 5]] - // [[2 4] [1 3 5]] - // [[1 2] [3 4] [5]] + // Output: + // [[1 2 3 4 5]] + // [[2 4] [1 3 5]] + // [[1 2] [3 4] [5]] } ``` @@ -2507,13 +2556,13 @@ import ( ) func main() { - nums := []int{1, 2, 3, 4, 5} + nums := []int{1, 2, 3, 4, 5} - val, idx := slice.Random(nums) - if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { - fmt.Println("okk") - } - // Output: - // okk + val, idx := slice.Random(nums) + if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { + fmt.Println("okk") + } + // Output: + // okk } ``` \ No newline at end of file diff --git a/slice/slice.go b/slice/slice.go index 6ac0a15..23bb2f7 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -618,35 +618,34 @@ func IntSlice(slice any) []int { return result } -// DeleteAt delete the element of slice from start index to end index - 1. +// DeleteAt delete the element of slice at index. // Play: https://go.dev/play/p/pJ-d6MUWcvK -func DeleteAt[T any](slice []T, start int, end ...int) []T { - size := len(slice) - - if start < 0 || start >= size { - return slice +func DeleteAt[T any](slice []T, index int) []T { + if index >= len(slice) { + index = len(slice) - 1 } - if len(end) > 0 { - end := end[0] - if end <= start { - return slice - } - if end > size { - end = size - } + result := make([]T, len(slice)-1) + copy(result, slice[:index]) + copy(result[index:], slice[index+1:]) - slice = append(slice[:start], slice[end:]...) - return slice + return result +} + +// DeleteRange delete the element of slice from start index to end index(exclude). +// Play: todo +func DeleteRange[T any](slice []T, start, end int) []T { + result := make([]T, 0, len(slice)-(end-start)) + + for i := 0; i < start; i++ { + result = append(result, slice[i]) } - if start == size-1 { - slice = slice[:start] - } else { - slice = append(slice[:start], slice[start+1:]...) + for i := end; i < len(slice); i++ { + result = append(result, slice[i]) } - return slice + return result } // Drop drop n elements from the start of a slice. diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index e495634..7d6bb2f 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -594,18 +594,46 @@ func ExampleIntSlice() { } func ExampleDeleteAt() { - result1 := DeleteAt([]string{"a", "b", "c"}, -1) - result2 := DeleteAt([]string{"a", "b", "c"}, 0) - result3 := DeleteAt([]string{"a", "b", "c"}, 0, 2) + chars := []string{"a", "b", "c", "d", "e"} + + result1 := DeleteAt(chars, 0) + result2 := DeleteAt(chars, 4) + result3 := DeleteAt(chars, 5) + result4 := DeleteAt(chars, 6) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + fmt.Println(result4) // Output: - // [a b c] - // [b c] - // [c] + // [b c d e] + // [a b c d] + // [a b c d] + // [a b c d] +} + +func ExampleDeleteRange() { + chars := []string{"a", "b", "c", "d", "e"} + + result1 := DeleteRange(chars, 0, 0) + result2 := DeleteRange(chars, 0, 1) + result3 := DeleteRange(chars, 0, 3) + result4 := DeleteRange(chars, 0, 4) + result5 := DeleteRange(chars, 0, 5) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // [a b c d e] + // [b c d e] + // [d e] + // [e] + // [] } func ExampleDrop() { diff --git a/slice/slice_test.go b/slice/slice_test.go index 5f3c81c..59b0c39 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -572,19 +572,27 @@ func TestDeleteAt(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestDeleteAt") + arr := []int{1, 2, 3, 4, 5} - assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, -1)) - assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, 3)) - assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0)) - assert.Equal([]string{"a", "c"}, DeleteAt([]string{"a", "b", "c"}, 1)) - assert.Equal([]string{"a", "b"}, DeleteAt([]string{"a", "b", "c"}, 2)) + assert.Equal([]int{2, 3, 4, 5}, DeleteAt(arr, 0)) + assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 4)) - assert.Equal([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0, 1)) - assert.Equal([]string{"c"}, DeleteAt([]string{"a", "b", "c"}, 0, 2)) - assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 3)) - assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 4)) - assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 3)) - assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 4)) + assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 5)) + assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 6)) + + assert.Equal([]int{1, 2, 3, 4, 5}, arr) +} + +func TestDeleteRange(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestDeleteRange") + + arr := []int{1, 2, 3, 4, 5} + + assert.Equal([]int{1, 2, 3, 4, 5}, DeleteRange(arr, 0, 0)) + assert.Equal([]int{2, 3, 4, 5}, DeleteRange(arr, 0, 1)) + assert.Equal([]int{4, 5}, DeleteRange(arr, 0, 3)) } func TestDrop(t *testing.T) {