Skip to content

Commit

Permalink
Add new method Concat (#376)
Browse files Browse the repository at this point in the history
* Add new method Concat

---------

Co-authored-by: Zhang Jie (Jason) <jie.zhang@grabtaxi.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
  • Loading branch information
3 people committed Jun 29, 2024
1 parent fe16ff7 commit 940ded8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Supported helpers for slices:
- [CountValuesBy](#countvaluesby)
- [Subset](#subset)
- [Slice](#slice)
- [Concat](#concat)
- [Replace](#replace)
- [ReplaceAll](#replaceall)
- [Compact](#compact)
Expand Down Expand Up @@ -906,6 +907,21 @@ slice := lo.Slice(in, 4, 3)

[[play](https://go.dev/play/p/8XWYhfMMA1h)]

### Concat

Returns a new slice containing all the elements in collections. Concat conserves the order of the elements.

```go
slice := lo.Concat([]int{1, 2}, []int{3, 4})
// []int{1, 2, 3, 4}

slice := lo.Concat(nil, []int{1, 2}, nil, []int{3, 4}, nil)
// []int{1, 2, 3, 4}

slice := lo.Concat[int]()
// []int{}
```

### Replace

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
Expand Down
17 changes: 17 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,23 @@ func Slice[T any](collection []T, start int, end int) []T {
return collection[start:end]
}

// Concat returns a new slice containing all the elements in
// collections. Concat conserves the order of the elements.
func Concat[T any](collections ...[]T) []T {
size := 0
for i := range collections {
size += len(collections[i])
}

result := make([]T, 0, size) // preallocate memory for the output slice
for i := 0; i < len(collections); i++ {
// `result` memory address is not expected to change, because we preallocated enough memory
result = append(result, collections[i]...)
}

return result
}

// 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/XfPzmf9gql6
func Replace[T comparable](collection []T, old T, new T, n int) []T {
Expand Down
10 changes: 10 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,16 @@ func TestSlice(t *testing.T) {
is.Equal([]int{0, 1, 2, 3, 4}, out18)
}

func TestConcat(t *testing.T) {
t.Parallel()
is := assert.New(t)

is.Equal([]int{1, 2, 3, 4}, Concat([]int{1, 2}, []int{3, 4}))
is.Equal([]int{1, 2, 3, 4}, Concat(nil, []int{1, 2}, nil, []int{3, 4}, nil))
is.Equal([]int{}, Concat[int](nil, nil))
is.Equal([]int{}, Concat[int]())
}

func TestReplace(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down

0 comments on commit 940ded8

Please sign in to comment.