Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new method Concat #376

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Supported helpers for slices:
- [CountValuesBy](#countvaluesby)
- [Subset](#subset)
- [Slice](#slice)
- [Concat](#concat)
- [Replace](#replace)
- [ReplaceAll](#replaceall)
- [Compact](#compact)
Expand Down Expand Up @@ -848,6 +849,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 @@ -529,6 +529,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
14 changes: 12 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func TestAssociate(t *testing.T) {

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

type foo struct {
baz string
bar int
Expand Down Expand Up @@ -626,7 +626,7 @@ func TestSlice(t *testing.T) {
out16 := Slice(in, -10, 1)
out17 := Slice(in, -1, 3)
out18 := Slice(in, -10, 7)

is.Equal([]int{}, out1)
is.Equal([]int{0}, out2)
is.Equal([]int{0, 1, 2, 3, 4}, out3)
Expand All @@ -647,6 +647,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
Loading