mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 21:02:27 +08:00
feat: refact DeleteAt and add DeleteRange function
This commit is contained in:
@@ -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() {
|
||||
|
||||
### <span id="DeleteAt">DeleteAt</span>
|
||||
|
||||
<p>删除切片中指定开始索引到结束索引的元素</p>
|
||||
<p>删除切片中指定索引的元素(不修改原切片)。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```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>
|
||||
@@ -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]
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### <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() {
|
||||
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
|
||||
}
|
||||
```
|
||||
@@ -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() {
|
||||
|
||||
### <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>
|
||||
|
||||
```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>
|
||||
@@ -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]
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="DeleteRange">DeleteRange</span>
|
||||
|
||||
<p>Delete the element of slice from start index to end index(exclude)</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() {
|
||||
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
|
||||
}
|
||||
```
|
||||
@@ -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.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user