feat: deprecate lo.Reverse and move it to lom.Reverse

This commit is contained in:
Samuel Berthe
2025-01-25 18:25:26 +01:00
parent 8e64522eb6
commit 153e0790c3
5 changed files with 86 additions and 11 deletions

View File

@@ -651,7 +651,12 @@ Reverses array so that the first element becomes the last, the second element be
⚠️ This helper is **mutable**. This behavior might change in `v2.0.0`. See [#160](https://github.com/samber/lo/issues/160).
```go
reverseOrder := lo.Reverse([]int{0, 1, 2, 3, 4, 5})
import lom "github.com/samber/lo/mutable"
list := []int{0, 1, 2, 3, 4, 5}
lom.Reverse(list)
list
// []int{5, 4, 3, 2, 1, 0}
```

13
mutable/slice.go Normal file
View File

@@ -0,0 +1,13 @@
package mutable
// 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
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]
}
}

View File

@@ -0,0 +1,20 @@
package mutable
import "fmt"
func ExampleReverse() {
list := []int{0, 1, 2, 3, 4, 5}
Reverse(list)
fmt.Printf("%v", list)
// Output: [5 4 3 2 1 0]
}
// Fill fills elements of array with `initial` value.
// Play: https://go.dev/play/p/VwR34GzqEub
func Fill[T any, Slice ~[]T](collection Slice, initial T) {
for i := range collection {
collection[i] = initial
}
}

42
mutable/slice_test.go Normal file
View File

@@ -0,0 +1,42 @@
package mutable
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestReverse(t *testing.T) {
t.Parallel()
is := assert.New(t)
list := []int{0, 1, 2, 3, 4, 5}
Reverse(list)
is.Equal(list, []int{5, 4, 3, 2, 1, 0})
list = []int{0, 1, 2, 3, 4, 5, 6}
Reverse(list)
is.Equal(list, []int{6, 5, 4, 3, 2, 1, 0})
list = []int{}
Reverse(list)
is.Equal(list, []int{})
type myStrings []string
allStrings := myStrings{"", "foo", "bar"}
Reverse(allStrings)
is.IsType(myStrings{"", "foo", "bar"}, allStrings, "type preserved")
}
func TestFill(t *testing.T) {
t.Parallel()
is := assert.New(t)
list1 := []string{"a", "0"}
Fill(list1, "b")
is.Equal([]string{"b", "b"}, list1)
list2 := []string{}
Fill(list2, "b")
is.Equal([]string{}, list2)
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/samber/lo/internal/constraints"
"github.com/samber/lo/internal/rand"
"github.com/samber/lo/mutable"
)
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
@@ -308,22 +309,16 @@ func Shuffle[T any, Slice ~[]T](collection Slice) Slice {
// 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/fhUMLvZ7vS6
// Deprecated: use mutable.Reverse() instead.
func Reverse[T any, Slice ~[]T](collection Slice) 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]
}
mutable.Reverse(collection)
return collection
}
// Fill fills elements of array with `initial` value.
// Play: https://go.dev/play/p/VwR34GzqEub
func Fill[T Clonable[T]](collection []T, initial T) []T {
result := make([]T, 0, len(collection))
func Fill[T Clonable[T], Slice ~[]T](collection Slice, initial T) Slice {
result := make(Slice, 0, len(collection))
for range collection {
result = append(result, initial.Clone())