1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: refact DeleteAt and add DeleteRange function

This commit is contained in:
dudaodong
2024-01-22 15:34:35 +08:00
parent bbc58c7e46
commit d2d1e5a055
5 changed files with 221 additions and 88 deletions

View File

@@ -35,6 +35,7 @@ import (
- [DifferenceBy](#DifferenceBy) - [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith) - [DifferenceWith](#DifferenceWith)
- [DeleteAt](#DeleteAt) - [DeleteAt](#DeleteAt)
- [DeleteRange](#DeleteRange)
- [Drop](#Drop) - [Drop](#Drop)
- [DropRight](#DropRight) - [DropRight](#DropRight)
- [DropWhile](#DropWhile) - [DropWhile](#DropWhile)
@@ -516,12 +517,12 @@ func main() {
### <span id="DeleteAt">DeleteAt</span> ### <span id="DeleteAt">DeleteAt</span>
<p>删除切片中指定开始索引到结束索引的元素</p> <p>删除切片中指定索引的元素(不修改原切片)。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func DeleteAt[T any](slice []T, start int, end ...int) func DeleteAt[T any](slice []T, index int) []T
``` ```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pJ-d6MUWcvK)</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pJ-d6MUWcvK)</span></b>
@@ -533,18 +534,66 @@ import (
) )
func main() { func main() {
result1 := slice.DeleteAt([]string{"a", "b", "c"}, -1) chars := []string{"a", "b", "c", "d", "e"}
result2 := slice.DeleteAt([]string{"a", "b", "c"}, 0)
result3 := slice.DeleteAt([]string{"a", "b", "c"}, 0, 2) 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(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4)
// Output: // Output:
// [a b c] // [b c d e]
// [b c] // [a b c d]
// [c] // [a b c d]
// [a b c d]
}
```
### <span id="DeleteRange">DeleteRange</span>
<p>删除切片中指定索引范围的元素(不修改原切片)。</p>
<b>函数签名:</b>
```go
func DeleteRange[T any](slice []T, start, end int) []T
```
<b>示例:</b>
```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() { func main() {
nums := []int{1, 2, 3, 4, 5} nums := []int{1, 2, 3, 4, 5}
result1 := slice.Partition(nums) result1 := slice.Partition(nums)
result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) 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 }) 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(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// [[1 2 3 4 5]] // [[1 2 3 4 5]]
// [[2 4] [1 3 5]] // [[2 4] [1 3 5]]
// [[1 2] [3 4] [5]] // [[1 2] [3 4] [5]]
} }
``` ```
@@ -2510,13 +2559,13 @@ import (
) )
func main() { func main() {
nums := []int{1, 2, 3, 4, 5} nums := []int{1, 2, 3, 4, 5}
val, idx := slice.Random(nums) val, idx := slice.Random(nums)
if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) {
fmt.Println("okk") fmt.Println("okk")
} }
// Output: // Output:
// okk // okk
} }
``` ```

View File

@@ -35,6 +35,7 @@ import (
- [DifferenceBy](#DifferenceBy) - [DifferenceBy](#DifferenceBy)
- [DifferenceWith](#DifferenceWith) - [DifferenceWith](#DifferenceWith)
- [DeleteAt](#DeleteAt) - [DeleteAt](#DeleteAt)
- [DeleteRange](#DeleteRange)
- [Drop](#Drop) - [Drop](#Drop)
- [DropRight](#DropRight) - [DropRight](#DropRight)
- [DropWhile](#DropWhile) - [DropWhile](#DropWhile)
@@ -515,12 +516,12 @@ func main() {
### <span id="DeleteAt">DeleteAt</span> ### <span id="DeleteAt">DeleteAt</span>
<p>Delete the element of slice from start index to end index - 1.</p> <p>Delete delete the element of slice at index.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func DeleteAt[T any](slice []T, start int, end ...int) func DeleteAt[T any](slice []T, index int)
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/pJ-d6MUWcvK)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/pJ-d6MUWcvK)</span></b>
@@ -532,18 +533,66 @@ import (
) )
func main() { func main() {
result1 := slice.DeleteAt([]string{"a", "b", "c"}, -1) chars := []string{"a", "b", "c", "d", "e"}
result2 := slice.DeleteAt([]string{"a", "b", "c"}, 0)
result3 := slice.DeleteAt([]string{"a", "b", "c"}, 0, 2) 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(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4)
// Output: // Output:
// [a b c] // [b c d e]
// [b c] // [a b c d]
// [c] // [a b c d]
// [a b c d]
}
```
### <span id="DeleteRange">DeleteRange</span>
<p>Delete the element of slice from start index to end indexexclude)</p>
<b>Signature:</b>
```go
func DeleteRange[T any](slice []T, start, end int) []T
```
<b>Example:</b>
```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() { func main() {
nums := []int{1, 2, 3, 4, 5} nums := []int{1, 2, 3, 4, 5}
result1 := slice.Partition(nums) result1 := slice.Partition(nums)
result2 := slice.Partition(nums, func(n int) bool { return n%2 == 0 }) 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 }) 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(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// [[1 2 3 4 5]] // [[1 2 3 4 5]]
// [[2 4] [1 3 5]] // [[2 4] [1 3 5]]
// [[1 2] [3 4] [5]] // [[1 2] [3 4] [5]]
} }
``` ```
@@ -2507,13 +2556,13 @@ import (
) )
func main() { func main() {
nums := []int{1, 2, 3, 4, 5} nums := []int{1, 2, 3, 4, 5}
val, idx := slice.Random(nums) val, idx := slice.Random(nums)
if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) { if idx >= 0 && idx < len(nums) && slice.Contain(nums, val) {
fmt.Println("okk") fmt.Println("okk")
} }
// Output: // Output:
// okk // okk
} }
``` ```

View File

@@ -618,35 +618,34 @@ func IntSlice(slice any) []int {
return result 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 // Play: https://go.dev/play/p/pJ-d6MUWcvK
func DeleteAt[T any](slice []T, start int, end ...int) []T { func DeleteAt[T any](slice []T, index int) []T {
size := len(slice) if index >= len(slice) {
index = len(slice) - 1
if start < 0 || start >= size {
return slice
} }
if len(end) > 0 { result := make([]T, len(slice)-1)
end := end[0] copy(result, slice[:index])
if end <= start { copy(result[index:], slice[index+1:])
return slice
}
if end > size {
end = size
}
slice = append(slice[:start], slice[end:]...) return result
return slice }
// DeleteRange delete the element of slice from start index to end indexexclude).
// 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 { for i := end; i < len(slice); i++ {
slice = slice[:start] result = append(result, slice[i])
} else {
slice = append(slice[:start], slice[start+1:]...)
} }
return slice return result
} }
// Drop drop n elements from the start of a slice. // Drop drop n elements from the start of a slice.

View File

@@ -594,18 +594,46 @@ func ExampleIntSlice() {
} }
func ExampleDeleteAt() { func ExampleDeleteAt() {
result1 := DeleteAt([]string{"a", "b", "c"}, -1) chars := []string{"a", "b", "c", "d", "e"}
result2 := DeleteAt([]string{"a", "b", "c"}, 0)
result3 := DeleteAt([]string{"a", "b", "c"}, 0, 2) result1 := DeleteAt(chars, 0)
result2 := DeleteAt(chars, 4)
result3 := DeleteAt(chars, 5)
result4 := DeleteAt(chars, 6)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4)
// Output: // Output:
// [a b c] // [b c d e]
// [b c] // [a b c d]
// [c] // [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() { func ExampleDrop() {

View File

@@ -572,19 +572,27 @@ func TestDeleteAt(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestDeleteAt") 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([]int{2, 3, 4, 5}, DeleteAt(arr, 0))
assert.Equal([]string{"a", "b", "c"}, DeleteAt([]string{"a", "b", "c"}, 3)) assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 4))
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([]string{"b", "c"}, DeleteAt([]string{"a", "b", "c"}, 0, 1)) assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 5))
assert.Equal([]string{"c"}, DeleteAt([]string{"a", "b", "c"}, 0, 2)) assert.Equal([]int{1, 2, 3, 4}, DeleteAt(arr, 6))
assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 3))
assert.Equal([]string{}, DeleteAt([]string{"a", "b", "c"}, 0, 4)) assert.Equal([]int{1, 2, 3, 4, 5}, arr)
assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 3)) }
assert.Equal([]string{"a"}, DeleteAt([]string{"a", "b", "c"}, 1, 4))
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) { func TestDrop(t *testing.T) {