Skip to content

Commit

Permalink
fix: chunk memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir20 committed Jul 15, 2024
1 parent 34537d5 commit 07361af
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice {
if last > len(collection) {
last = len(collection)
}
result = append(result, collection[i*size:last])
result = append(result, collection[i*size:last:last])
}

return result
Expand Down
6 changes: 6 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ func TestChunk(t *testing.T) {
allStrings := myStrings{"", "foo", "bar"}
nonempty := Chunk(allStrings, 2)
is.IsType(nonempty[0], allStrings, "type preserved")

// appending to a chunk should not affect original array
originalArray := []int{0, 1, 2, 3, 4, 5}
result5 := Chunk(originalArray, 2)
result5[0] = append(result5[0], 6)
is.Equal(originalArray, []int{0, 1, 2, 3, 4, 5})
}

func TestPartitionBy(t *testing.T) {
Expand Down

0 comments on commit 07361af

Please sign in to comment.