mirror of
https://github.com/samber/lo.git
synced 2025-10-05 07:56:51 +08:00
24 lines
758 B
Go
24 lines
758 B
Go
package mutable
|
|
|
|
import "github.com/samber/lo/internal/rand"
|
|
|
|
// Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
|
|
// Play: https://go.dev/play/p/2xb3WdLjeSJ
|
|
func Shuffle[T any, Slice ~[]T](collection Slice) {
|
|
rand.Shuffle(len(collection), func(i, j int) {
|
|
collection[i], collection[j] = collection[j], collection[i]
|
|
})
|
|
}
|
|
|
|
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
|
|
// Play: https://go.dev/play/p/O-M5pmCRgzV
|
|
func Reverse[T any, Slice ~[]T](collection Slice) {
|
|
length := len(collection)
|
|
half := length / 2
|
|
|
|
for i := 0; i < half; i = i + 1 {
|
|
j := length - 1 - i
|
|
collection[i], collection[j] = collection[j], collection[i]
|
|
}
|
|
}
|