mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
doc: add doc for Drop function
This commit is contained in:
148
docs/slice.md
148
docs/slice.md
@@ -35,6 +35,9 @@ import (
|
|||||||
- [DifferenceWith](#DifferenceWith)
|
- [DifferenceWith](#DifferenceWith)
|
||||||
- [DeleteAt](#DeleteAt)
|
- [DeleteAt](#DeleteAt)
|
||||||
- [Drop](#Drop)
|
- [Drop](#Drop)
|
||||||
|
- [DropRight](#DropRight)
|
||||||
|
- [DropWhile](#DropWhile)
|
||||||
|
- [DropRightWhile](#DropRightWhile)
|
||||||
- [Equal](#Equal)
|
- [Equal](#Equal)
|
||||||
- [EqualWith](#EqualWith)
|
- [EqualWith](#EqualWith)
|
||||||
- [Every](#Every)
|
- [Every](#Every)
|
||||||
@@ -487,7 +490,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
<p>Drop n elements from the start of a slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -505,20 +508,139 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := slice.Drop([]string{"a", "b", "c"}, 0)
|
result1 := slice.Drop([]string{"a", "b", "c"}, 0)
|
||||||
result2 := slice.Drop([]string{"a", "b", "c"}, 1)
|
result2 := slice.Drop([]string{"a", "b", "c"}, 1)
|
||||||
result3 := slice.Drop([]string{"a", "b", "c"}, -1)
|
result3 := slice.Drop([]string{"a", "b", "c"}, -1)
|
||||||
result4 := slice.Drop([]string{"a", "b", "c"}, 4)
|
result4 := slice.Drop([]string{"a", "b", "c"}, 4)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [a b c]
|
// [a b c]
|
||||||
// [b c]
|
// [b c]
|
||||||
// [a b]
|
// [a b c]
|
||||||
// []
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropRight">DropRight</span>
|
||||||
|
|
||||||
|
<p>Drop n elements from the end of a slice.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropRight[T any](slice []T, n int) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.DropRight([]string{"a", "b", "c"}, 0)
|
||||||
|
result2 := slice.DropRight([]string{"a", "b", "c"}, 1)
|
||||||
|
result3 := slice.DropRight([]string{"a", "b", "c"}, -1)
|
||||||
|
result4 := slice.DropRight([]string{"a", "b", "c"}, 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c]
|
||||||
|
// [a b]
|
||||||
|
// [a b c]
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropWhile">DropWhile</span>
|
||||||
|
|
||||||
|
<p>Drop n elements from the start of a slice while predicate function returns true.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropWhile[T any](slice []T, predicate func(item T) bool) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return n != 2
|
||||||
|
})
|
||||||
|
result2 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
result3 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return n == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [2 3 4 5]
|
||||||
|
// []
|
||||||
|
// [1 2 3 4 5]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropRightWhile">DropRightWhile</span>
|
||||||
|
|
||||||
|
<p>Drop n elements from the end of a slice while predicate function returns true.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
result1 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return n != 2
|
||||||
|
})
|
||||||
|
result2 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
result3 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return n == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2]
|
||||||
|
// []
|
||||||
|
// [1 2 3 4 5]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ import (
|
|||||||
- [DifferenceWith](#DifferenceWith)
|
- [DifferenceWith](#DifferenceWith)
|
||||||
- [DeleteAt](#DeleteAt)
|
- [DeleteAt](#DeleteAt)
|
||||||
- [Drop](#Drop)
|
- [Drop](#Drop)
|
||||||
|
- [DropRight](#DropRight)
|
||||||
|
- [DropWhile](#DropWhile)
|
||||||
|
- [DropRightWhile](#DropRightWhile)
|
||||||
- [Every](#Every)
|
- [Every](#Every)
|
||||||
- [Equal](#Equal)
|
- [Equal](#Equal)
|
||||||
- [EqualWith](#EqualWith)
|
- [EqualWith](#EqualWith)
|
||||||
@@ -486,9 +489,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>创建一个切片,当n > 0时从开头删除n个元素,或者当n < 0时从结尾删除n个元素</p>
|
<p>从切片的头部删除n个元素。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -506,20 +510,139 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := slice.Drop([]string{"a", "b", "c"}, 0)
|
result1 := slice.Drop([]string{"a", "b", "c"}, 0)
|
||||||
result2 := slice.Drop([]string{"a", "b", "c"}, 1)
|
result2 := slice.Drop([]string{"a", "b", "c"}, 1)
|
||||||
result3 := slice.Drop([]string{"a", "b", "c"}, -1)
|
result3 := slice.Drop([]string{"a", "b", "c"}, -1)
|
||||||
result4 := slice.Drop([]string{"a", "b", "c"}, 4)
|
result4 := slice.Drop([]string{"a", "b", "c"}, 4)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [a b c]
|
// [a b c]
|
||||||
// [b c]
|
// [b c]
|
||||||
// [a b]
|
// [a b c]
|
||||||
// []
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropRight">DropRight</span>
|
||||||
|
|
||||||
|
<p>从切片的尾部删除n个元素。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropRight[T any](slice []T, n int) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.DropRight([]string{"a", "b", "c"}, 0)
|
||||||
|
result2 := slice.DropRight([]string{"a", "b", "c"}, 1)
|
||||||
|
result3 := slice.DropRight([]string{"a", "b", "c"}, -1)
|
||||||
|
result4 := slice.DropRight([]string{"a", "b", "c"}, 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c]
|
||||||
|
// [a b]
|
||||||
|
// [a b c]
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropWhile">DropWhile</span>
|
||||||
|
|
||||||
|
<p>从切片的头部删除n个元素,这个n个元素满足predicate函数返回true。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropWhile[T any](slice []T, predicate func(item T) bool) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return n != 2
|
||||||
|
})
|
||||||
|
result2 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
result3 := slice.DropWhile(numbers, func(n int) bool {
|
||||||
|
return n == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [2 3 4 5]
|
||||||
|
// []
|
||||||
|
// [1 2 3 4 5]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="DropRightWhile">DropRightWhile</span>
|
||||||
|
|
||||||
|
<p>从切片的尾部删除n个元素,这个n个元素满足predicate函数返回true。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
result1 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return n != 2
|
||||||
|
})
|
||||||
|
result2 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
result3 := slice.DropRightWhile(numbers, func(n int) bool {
|
||||||
|
return n == 0
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2]
|
||||||
|
// []
|
||||||
|
// [1 2 3 4 5]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user