1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add ReverseCopy

This commit is contained in:
dudaodong
2025-02-14 16:23:46 +08:00
parent 23e61f1acf
commit 2e619e48a3
5 changed files with 103 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ import (
- [FlatMap](#FlatMap)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [ReverseCopy](#ReverseCopy)
- [Reduce<sup>deprecated</sup>](#Reduce)
- [ReduceConcurrent](#ReduceConcurrent)
- [ReduceBy](#ReduceBy)
@@ -1760,6 +1761,38 @@ func main() {
}
```
### <span id="ReverseCopy">ReverseCopy</span>
<p>反转切片中的元素顺序, 不改变原slice。</p>
<b>函数签名:</b>
```go
func ReverseCopy[T any](slice []T) []T
```
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "c", "d"}
reversedStrs := slice.ReverseCopy(strs)
fmt.Println(reversedStrs)
fmt.Println(strs)
// Output:
// [d c b a]
// [a b c d]
}
```
### <span id="Reduce">Reduce</span>
<p>将切片中的元素依次运行iteratee函数返回运行结果。</p>

View File

@@ -70,6 +70,7 @@ import (
- [FlatMap](#FlatMap)
- [Merge](#Merge)
- [Reverse](#Reverse)
- [ReverseCopy](#ReverseCopy)
- [Reduce<sup>deprecated</sup>](#Reduce)
- [ReduceConcurrent](#ReduceConcurrent)
- [ReduceBy](#ReduceBy)
@@ -1756,6 +1757,38 @@ func main() {
}
```
### <span id="ReverseCopy">ReverseCopy</span>
<p>Return a new slice of element order is reversed to the given slice.</p>
<b>Signature:</b>
```go
func ReverseCopy[T any](slice []T) []T
```
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "c", "d"}
reversedStrs := slice.ReverseCopy(strs)
fmt.Println(reversedStrs)
fmt.Println(strs)
// Output:
// [d c b a]
// [a b c d]
}
```
### <span id="Reduce">Reduce</span>
<p>Reduce slice.</p>

View File

@@ -1002,7 +1002,19 @@ func Reverse[T any](slice []T) {
}
}
// Shuffle the slice.
// ReverseCopy return a new slice of element order is reversed to the given slice.
// Play: todo
func ReverseCopy[T any](slice []T) []T {
result := make([]T, len(slice))
for i, j := 0, len(slice)-1; i < len(slice); i, j = i+1, j-1 {
result[i] = slice[j]
}
return result
}
// Shuffle return a new slice with elements shuffled.
// Play: https://go.dev/play/p/YHvhnWGU3Ge
func Shuffle[T any](slice []T) []T {
rand.Seed(time.Now().UnixNano())

View File

@@ -937,6 +937,19 @@ func ExampleReverse() {
// [d c b a]
}
func ExampleReverseCopy() {
strs := []string{"a", "b", "c", "d"}
reversedStrs := ReverseCopy(strs)
fmt.Println(reversedStrs)
fmt.Println(strs)
// Output:
// [d c b a]
// [a b c d]
}
func ExampleIsAscending() {
result1 := IsAscending([]int{1, 2, 3, 4, 5})

View File

@@ -1114,6 +1114,17 @@ func TestReverse(t *testing.T) {
assert.Equal([]string{"e", "d", "c", "b", "a"}, s2)
}
func TestReverseCopy(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestReverseCopy")
numbers := []int{1, 2, 3, 4, 5}
reversedNumbers := ReverseCopy(numbers)
assert.Equal([]int{5, 4, 3, 2, 1}, reversedNumbers)
assert.Equal([]int{1, 2, 3, 4, 5}, numbers)
}
func TestDifference(t *testing.T) {
t.Parallel()