mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
2 Commits
8bdd46bda4
...
c01c9d14b4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c01c9d14b4 | ||
|
|
f198191063 |
@@ -63,6 +63,8 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [ReduceBy](#ReduceBy)
|
||||||
|
- [ReduceRight](#ReduceRight)
|
||||||
- [Replace](#Replace)
|
- [Replace](#Replace)
|
||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
@@ -1504,6 +1506,72 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceBy">ReduceBy</span>
|
||||||
|
|
||||||
|
<p>Produces a value from slice by accumulating the result of each element as passed through the reducer function.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := slice.ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceRight">ReduceRight</span>
|
||||||
|
|
||||||
|
<p>ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := slice.ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Replace">Replace</span>
|
### <span id="Replace">Replace</span>
|
||||||
|
|
||||||
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ import (
|
|||||||
- [Merge](#Merge)
|
- [Merge](#Merge)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [Reduce](#Reduce)
|
- [Reduce](#Reduce)
|
||||||
|
- [ReduceBy](#ReduceBy)
|
||||||
|
- [ReduceRight](#ReduceRight)
|
||||||
- [Replace](#Replace)
|
- [Replace](#Replace)
|
||||||
- [ReplaceAll](#ReplaceAll)
|
- [ReplaceAll](#ReplaceAll)
|
||||||
- [Repeat](#Repeat)
|
- [Repeat](#Repeat)
|
||||||
@@ -1505,6 +1507,72 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceBy">ReduceBy</span>
|
||||||
|
|
||||||
|
<p>对切片中执行reduce操作。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := slice.ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := slice.ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ReduceRight">ReduceRight</span>
|
||||||
|
|
||||||
|
<p>类似ReduceBy操作,迭代切片元素顺序从右至左。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result := slice.ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Replace">Replace</span>
|
### <span id="Replace">Replace</span>
|
||||||
|
|
||||||
<p>返回切片的副本,其中前n个不重叠的old替换为new</p>
|
<p>返回切片的副本,其中前n个不重叠的old替换为new</p>
|
||||||
|
|||||||
@@ -492,6 +492,30 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReduceBy produces a value from slice by accumulating the result of each element as passed through the reducer function.
|
||||||
|
// Play: todo
|
||||||
|
func ReduceBy[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
|
||||||
|
accumulator := initial
|
||||||
|
|
||||||
|
for i, v := range slice {
|
||||||
|
accumulator = reducer(i, v, accumulator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReduceRight is like ReduceBy, but it iterates over elements of slice from right to left.
|
||||||
|
// Play: todo
|
||||||
|
func ReduceRight[T any, U any](slice []T, initial U, reducer func(index int, item T, agg U) U) U {
|
||||||
|
accumulator := initial
|
||||||
|
|
||||||
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
|
accumulator = reducer(i, slice[i], accumulator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
|
|
||||||
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
|
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
|
||||||
// Play: https://go.dev/play/p/P5mZp7IhOFo
|
// Play: https://go.dev/play/p/P5mZp7IhOFo
|
||||||
func Replace[T comparable](slice []T, old T, new T, n int) []T {
|
func Replace[T comparable](slice []T, old T, new T, n int) []T {
|
||||||
|
|||||||
@@ -460,6 +460,34 @@ func ExampleReduce() {
|
|||||||
// 6
|
// 6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleReduceBy() {
|
||||||
|
result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 10
|
||||||
|
// 1234
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleReduceRight() {
|
||||||
|
result := ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4321
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleReplace() {
|
func ExampleReplace() {
|
||||||
strs := []string{"a", "b", "c", "a"}
|
strs := []string{"a", "b", "c", "a"}
|
||||||
|
|
||||||
|
|||||||
@@ -405,6 +405,32 @@ func TestReduce(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReduceBy(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestReduceBy")
|
||||||
|
|
||||||
|
result1 := ReduceBy([]int{1, 2, 3, 4}, 0, func(_ int, item int, agg int) int {
|
||||||
|
return agg + item
|
||||||
|
})
|
||||||
|
|
||||||
|
result2 := ReduceBy([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(10, result1)
|
||||||
|
assert.Equal("1234", result2)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReduceRight(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "ReduceRight")
|
||||||
|
|
||||||
|
result := ReduceRight([]int{1, 2, 3, 4}, "", func(_ int, item int, agg string) string {
|
||||||
|
return agg + fmt.Sprintf("%v", item)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal("4321", result)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntSlice(t *testing.T) {
|
func TestIntSlice(t *testing.T) {
|
||||||
var nums []any
|
var nums []any
|
||||||
nums = append(nums, 1, 2, 3)
|
nums = append(nums, 1, 2, 3)
|
||||||
|
|||||||
Reference in New Issue
Block a user