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

feat: add ConcatBy

This commit is contained in:
dudaodong
2024-10-24 15:38:12 +08:00
parent 2015d36b08
commit a4e89bd7c1
5 changed files with 184 additions and 0 deletions

View File

@@ -107,6 +107,7 @@ import (
- [LeftPadding](#LeftPadding)
- [Frequency](#Frequency)
- [JoinFunc](#JoinFunc)
- [ConcatBy](#ConcatBy)
<div STYLE="page-break-after: always;"></div>
@@ -3016,4 +3017,51 @@ func main() {
// Output:
// A, B, C
}
```
### <span id="ConcatBy">ConcatBy</span>
<p>将切片中的元素连接成一个值,使用指定的分隔符和连接器函数。</p>
<b>函数签名:</b>
```go
func ConcatBy[T any](slice []T, sep T, connector func(T, T) 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() {
type Person struct {
Name string
Age int
}
people := []Person{
{Name: "Alice", Age: 30},
{Name: "Bob", Age: 25},
{Name: "Charlie", Age: 35},
}
sep := Person{Name: " | ", Age: 0}
personConnector := func(a, b Person) Person {
return Person{Name: a.Name + b.Name, Age: a.Age + b.Age}
}
result := slice.ConcatBy(people, sep, personConnector)
fmt.Println(result.Name)
fmt.Println(result.Age)
// Output:
// Alice | Bob | Charlie
// 90
}
```