mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
feat: deprecate lo.Reverse and move it to lom.Reverse
This commit is contained in:
@@ -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
13
mutable/slice.go
Normal 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]
|
||||
}
|
||||
}
|
20
mutable/slice_example_test.go
Normal file
20
mutable/slice_example_test.go
Normal 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
42
mutable/slice_test.go
Normal 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)
|
||||
}
|
15
slice.go
15
slice.go
@@ -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())
|
||||
|
Reference in New Issue
Block a user