Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: modify mutable.shuffle example code and Go Playground links #595

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,14 @@ Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
```go
import lom "github.com/samber/lo/mutable"

randomOrder := lom.Shuffle([]int{0, 1, 2, 3, 4, 5})
list := []int{0, 1, 2, 3, 4, 5}
lom.Shuffle(list)

list
// []int{1, 4, 0, 3, 5, 2}
```

[[play](https://go.dev/play/p/ZTGG7OUCdnp)]
[[play](https://go.dev/play/p/2xb3WdLjeSJ)]

### Reverse

Expand All @@ -692,7 +695,7 @@ list
// []int{5, 4, 3, 2, 1, 0}
```

[[play](https://go.dev/play/p/iv2e9jslfBM)]
[[play](https://go.dev/play/p/O-M5pmCRgzV)]

### Fill

Expand Down
4 changes: 2 additions & 2 deletions mutable/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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/ZTGG7OUCdnp
// 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/iv2e9jslfBM
// Play: https://go.dev/play/p/O-M5pmCRgzV
func Reverse[T any, Slice ~[]T](collection Slice) {
length := len(collection)
half := length / 2
Expand Down
Loading