feat: add ShuffleCopy

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

No files matched your search

+14
View File
@@ -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 {