Skip to content

Commit

Permalink
feat: add ConcatBy
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Oct 24, 2024
1 parent 2015d36 commit a4e89bd
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import (
- [LeftPadding](#LeftPadding)
- [Frequency](#Frequency)
- [JoinFunc](#JoinFunc)
- [ConcatBy](#ConcatBy)


<div STYLE="page-break-after: always;"></div>
Expand Down Expand Up @@ -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
}
```
51 changes: 51 additions & 0 deletions docs/en/api/packages/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ import (
- [RightPadding](#RightPadding)
- [LeftPadding](#LeftPadding)
- [Frequency](#Frequency)
- [JoinFunc](#JoinFunc)
- [ConcatBy](#ConcatBy)



<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -3009,4 +3013,51 @@ func main() {
// Output:
// A, B, C
}
```

### <span id="ConcatBy">ConcatBy</span>

<p>Concats the elements of a slice into a single value using the provided separator and connector function.</p>

<b>Signature:</b>

```go
func ConcatBy[T any](slice []T, sep T, connector func(T, T) 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() {
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
}
```
19 changes: 19 additions & 0 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,3 +1411,22 @@ func JoinFunc[T any](slice []T, sep string, transform func(T) T) string {
}
return buf.String()
}

// ConcatBy concats the elements of a slice into a single value using the provided separator and connector function.
// Play: todo
func ConcatBy[T any](slice []T, sep T, connector func(T, T) T) T {
var result T

if len(slice) == 0 {
return result
}

for i, v := range slice {
result = connector(result, v)
if i < len(slice)-1 {
result = connector(result, sep)
}
}

return result
}
28 changes: 28 additions & 0 deletions slice/slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,3 +1272,31 @@ func ExampleJoinFunc() {
// Output:
// A, B, C
}

func ExampleConcatBy() {
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 := ConcatBy(people, sep, personConnector)

fmt.Println(result.Name)
fmt.Println(result.Age)

// Output:
// Alice | Bob | Charlie
// 90
}
38 changes: 38 additions & 0 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1856,3 +1856,41 @@ func TestJoinFunc(t *testing.T) {
assert.Equal(expected, result)
})
}

func TestConcatBy(t *testing.T) {
t.Parallel()

assert := internal.NewAssert(t, "TestConcatBy")

t.Run("Join strings", func(t *testing.T) {
result := ConcatBy([]string{"Hello", "World"}, ", ", func(a, b string) string {
return a + b
})

expected := "Hello, World"
assert.Equal(expected, result)
})

t.Run("Join Person struct", func(t *testing.T) {
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 := ConcatBy(people, sep, personConnector)

assert.Equal("Alice | Bob | Charlie", result.Name)
assert.Equal(90, result.Age)
})
}

0 comments on commit a4e89bd

Please sign in to comment.