Skip to content

Commit 699707a

Browse files
committed
feat(mutable): shuffle
1 parent 64e89e4 commit 699707a

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,18 +640,22 @@ interleaved := lo.Interleave([]int{1}, []int{2, 5, 8}, []int{3, 6}, []int{4, 7,
640640

641641
Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
642642

643+
⚠️ This helper is **mutable**.
644+
643645
```go
644-
randomOrder := lo.Shuffle([]int{0, 1, 2, 3, 4, 5})
646+
import lom "github.com/samber/lo/mutable"
647+
648+
randomOrder := lom.Shuffle([]int{0, 1, 2, 3, 4, 5})
645649
// []int{1, 4, 0, 3, 5, 2}
646650
```
647651

648-
[[play](https://go.dev/play/p/Qp73bnTDnc7)]
652+
[[play](https://go.dev/play/p/ZTGG7OUCdnp)]
649653

650654
### Reverse
651655

652656
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
653657

654-
⚠️ This helper is **mutable**. This behavior might change in `v2.0.0`. See [#160](https://github.com/samber/lo/issues/160).
658+
⚠️ This helper is **mutable**.
655659

656660
```go
657661
import lom "github.com/samber/lo/mutable"
@@ -663,7 +667,7 @@ list
663667
// []int{5, 4, 3, 2, 1, 0}
664668
```
665669

666-
[[play](https://go.dev/play/p/fhUMLvZ7vS6)]
670+
[[play](https://go.dev/play/p/iv2e9jslfBM)]
667671

668672
### Fill
669673

mutable/slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package mutable
22

3+
import "github.com/samber/lo/internal/rand"
4+
5+
// Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
6+
// Play: https://go.dev/play/p/ZTGG7OUCdnp
7+
func Shuffle[T any, Slice ~[]T](collection Slice) {
8+
rand.Shuffle(len(collection), func(i, j int) {
9+
collection[i], collection[j] = collection[j], collection[i]
10+
})
11+
}
12+
313
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
414
// Play: https://go.dev/play/p/iv2e9jslfBM
515
func Reverse[T any, Slice ~[]T](collection Slice) {

mutable/slice_example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ package mutable
22

33
import "fmt"
44

5+
func ExampleShuffle() {
6+
list := []int{0, 1, 2, 3, 4, 5}
7+
8+
Shuffle(list)
9+
10+
fmt.Printf("%v", list)
11+
}
12+
513
func ExampleReverse() {
614
list := []int{0, 1, 2, 3, 4, 5}
715

mutable/slice_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9+
func TestShuffle(t *testing.T) {
10+
t.Parallel()
11+
is := assert.New(t)
12+
13+
list := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
14+
Shuffle(list)
15+
is.NotEqual(list, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
16+
17+
list = []int{}
18+
Shuffle(list)
19+
is.Equal(list, []int{})
20+
}
21+
922
func TestReverse(t *testing.T) {
1023
t.Parallel()
1124
is := assert.New(t)

slice.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"sort"
55

66
"github.com/samber/lo/internal/constraints"
7-
"github.com/samber/lo/internal/rand"
87
"github.com/samber/lo/mutable"
98
)
109

@@ -298,17 +297,17 @@ func Interleave[T any, Slice ~[]T](collections ...Slice) Slice {
298297
}
299298

300299
// Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
301-
// Play: https://go.dev/play/p/Qp73bnTDnc7
300+
// Play: https://go.dev/play/p/ZTGG7OUCdnp
301+
//
302+
// Deprecated: use mutable.Shuffle() instead.
302303
func Shuffle[T any, Slice ~[]T](collection Slice) Slice {
303-
rand.Shuffle(len(collection), func(i, j int) {
304-
collection[i], collection[j] = collection[j], collection[i]
305-
})
306-
304+
mutable.Shuffle(collection)
307305
return collection
308306
}
309307

310308
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
311-
// Play: https://go.dev/play/p/fhUMLvZ7vS6
309+
// Play: https://go.dev/play/p/iv2e9jslfBM
310+
//
312311
// Deprecated: use mutable.Reverse() instead.
313312
func Reverse[T any, Slice ~[]T](collection Slice) Slice {
314313
mutable.Reverse(collection)

0 commit comments

Comments
 (0)