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

feat: add ShuffleCopy

This commit is contained in:
dudaodong
2025-02-14 16:33:22 +08:00
parent 2e619e48a3
commit 0e7297cb97
5 changed files with 113 additions and 1 deletions

View File

@@ -79,6 +79,7 @@ import (
- [ReplaceAll](#ReplaceAll)
- [Repeat](#Repeat)
- [Shuffle](#Shuffle)
- [ShuffleCopy](#ShuffleCopy)
- [IsAscending](#IsAscending)
- [IsDescending](#IsDescending)
- [IsSorted](#IsSorted)
@@ -2027,7 +2028,7 @@ func main() {
### <span id="Shuffle">Shuffle</span>
<p>随机打乱切片中的元素顺序</p>
<p>随机打乱切片中的元素顺序</p>
<b>函数签名:</b>
@@ -2054,6 +2055,37 @@ func main() {
}
```
### <span id="ShuffleCopy">ShuffleCopy</span>
<p>随机打乱切片中的元素顺序, 不改变原切片。</p>
<b>函数签名:</b>
```go
func ShuffleCopy[T any](slice []T) []T
```
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
result := slice.ShuffleCopy(nums)
fmt.Println(result)
fmt.Println(nums)
// Output:
// [3 1 5 4 2] (random order)
// [1 2 3 4 5]
}
```
### <span id="IsAscending">IsAscending</span>
<p>检查切片元素是否按升序排列。</p>