diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 38093ab..b09ab17 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -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() { ### Shuffle -
随机打乱切片中的元素顺序
+随机打乱切片中的元素顺序。
函数签名: @@ -2054,6 +2055,37 @@ func main() { } ``` +### ShuffleCopy + +随机打乱切片中的元素顺序, 不改变原切片。
+ +函数签名: + +```go +func ShuffleCopy[T any](slice []T) []T +``` + +示例:[运行](todo) + +```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] +} +``` + ### IsAscending检查切片元素是否按升序排列。
diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 1578383..9f9d092 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -79,6 +79,7 @@ import ( - [ReplaceAll](#ReplaceAll) - [Repeat](#Repeat) - [Shuffle](#Shuffle) +- [ShuffleCopy](#ShuffleCopy) - [IsAscending](#IsAscending) - [IsDescending](#IsDescending) - [IsSorted](#IsSorted) @@ -2051,6 +2052,37 @@ func main() { } ``` +### ShuffleCopy + +Return a new slice with elements shuffled.
+ +Signature: + +```go +func ShuffleCopy[T any](slice []T) []T +``` + +Example:[Run](todo) + +```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] +} +``` + ### IsAscendingChecks if a slice is ascending order.
diff --git a/slice/slice.go b/slice/slice.go index f8da44c..6a27b51 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1026,6 +1026,20 @@ func Shuffle[T any](slice []T) []T { 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. // Play: https://go.dev/play/p/9CtsFjet4SH func IsAscending[T constraints.Ordered](slice []T) bool { diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index 182845d..339abdf 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -950,6 +950,28 @@ func ExampleReverseCopy() { // [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() { result1 := IsAscending([]int{1, 2, 3, 4, 5}) diff --git a/slice/slice_test.go b/slice/slice_test.go index 1c340e1..f6999dd 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -1340,6 +1340,18 @@ func TestShuffle(t *testing.T) { 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) { t.Parallel()