mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 17:52:28 +08:00
feat: add ShuffleCopy
This commit is contained in:
@@ -79,6 +79,7 @@ import (
|
|||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
|
- [ShuffleCopy](#ShuffleCopy)
|
||||||
- [IsAscending](#IsAscending)
|
- [IsAscending](#IsAscending)
|
||||||
- [IsDescending](#IsDescending)
|
- [IsDescending](#IsDescending)
|
||||||
- [IsSorted](#IsSorted)
|
- [IsSorted](#IsSorted)
|
||||||
@@ -2027,7 +2028,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="Shuffle">Shuffle</span>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
|
|
||||||
<p>随机打乱切片中的元素顺序</p>
|
<p>随机打乱切片中的元素顺序。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<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>
|
### <span id="IsAscending">IsAscending</span>
|
||||||
|
|
||||||
<p>检查切片元素是否按升序排列。</p>
|
<p>检查切片元素是否按升序排列。</p>
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ import (
|
|||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
|
- [ShuffleCopy](#ShuffleCopy)
|
||||||
- [IsAscending](#IsAscending)
|
- [IsAscending](#IsAscending)
|
||||||
- [IsDescending](#IsDescending)
|
- [IsDescending](#IsDescending)
|
||||||
- [IsSorted](#IsSorted)
|
- [IsSorted](#IsSorted)
|
||||||
@@ -2051,6 +2052,37 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ShuffleCopy">ShuffleCopy</span>
|
||||||
|
|
||||||
|
<p>Return a new slice with elements shuffled.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ShuffleCopy[T any](slice []T) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](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>
|
### <span id="IsAscending">IsAscending</span>
|
||||||
|
|
||||||
<p>Checks if a slice is ascending order.</p>
|
<p>Checks if a slice is ascending order.</p>
|
||||||
|
|||||||
@@ -1026,6 +1026,20 @@ func Shuffle[T any](slice []T) []T {
|
|||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShuffleCopy return a new slice with elements shuffled.
|
||||||
|
// Play: todo
|
||||||
|
func ShuffleCopy[T any](slice []T) []T {
|
||||||
|
result := make([]T, len(slice))
|
||||||
|
copy(result, slice)
|
||||||
|
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
rand.Shuffle(len(result), func(i, j int) {
|
||||||
|
result[i], result[j] = result[j], result[i]
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// IsAscending checks if a slice is ascending order.
|
// IsAscending checks if a slice is ascending order.
|
||||||
// Play: https://go.dev/play/p/9CtsFjet4SH
|
// Play: https://go.dev/play/p/9CtsFjet4SH
|
||||||
func IsAscending[T constraints.Ordered](slice []T) bool {
|
func IsAscending[T constraints.Ordered](slice []T) bool {
|
||||||
|
|||||||
@@ -950,6 +950,28 @@ func ExampleReverseCopy() {
|
|||||||
// [a b c d]
|
// [a b c d]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleShuffle() {
|
||||||
|
strs := []string{"a", "b", "c", "d"}
|
||||||
|
Shuffle(strs)
|
||||||
|
|
||||||
|
fmt.Println(len(strs))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleShuffleCopy() {
|
||||||
|
strs := []string{"a", "b", "c", "d"}
|
||||||
|
shuffledStrs := ShuffleCopy(strs)
|
||||||
|
|
||||||
|
fmt.Println(len(shuffledStrs))
|
||||||
|
fmt.Println(strs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// [a b c d]
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleIsAscending() {
|
func ExampleIsAscending() {
|
||||||
|
|
||||||
result1 := IsAscending([]int{1, 2, 3, 4, 5})
|
result1 := IsAscending([]int{1, 2, 3, 4, 5})
|
||||||
|
|||||||
@@ -1340,6 +1340,18 @@ func TestShuffle(t *testing.T) {
|
|||||||
assert.Equal(5, len(result))
|
assert.Equal(5, len(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShuffleCopy(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestShuffleCopy")
|
||||||
|
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
result := ShuffleCopy(numbers)
|
||||||
|
|
||||||
|
assert.Equal(5, len(result))
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 5}, numbers)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexOf(t *testing.T) {
|
func TestIndexOf(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user