diff --git a/README.md b/README.md index cabc79e..7d33b99 100644 --- a/README.md +++ b/README.md @@ -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} ``` diff --git a/mutable/slice.go b/mutable/slice.go new file mode 100644 index 0000000..136cf42 --- /dev/null +++ b/mutable/slice.go @@ -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] + } +} diff --git a/mutable/slice_example_test.go b/mutable/slice_example_test.go new file mode 100644 index 0000000..21b9456 --- /dev/null +++ b/mutable/slice_example_test.go @@ -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 + } +} diff --git a/mutable/slice_test.go b/mutable/slice_test.go new file mode 100644 index 0000000..67448fc --- /dev/null +++ b/mutable/slice_test.go @@ -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) +} diff --git a/slice.go b/slice.go index 7e6226a..d30c0d6 100644 --- a/slice.go +++ b/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())