diff --git a/slice/slice.go b/slice/slice.go index e3f39bb..0820b90 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -28,6 +28,9 @@ import ( // returned slice is: // // [6, 2, 8, 4] +// +// The capacity of the slice returned is clipped to its length, so that +// appending to it will not modify the elements of vs after those kept. func Partition[T any](vs []T, keep func(T) bool) []T { if len(vs) == 0 { return vs @@ -51,7 +54,7 @@ func Partition[T any](vs []T, keep func(T) bool) []T { // If the right cursor reached the end, we're done: Everything left of i // is kept, everything ≥ i is unkept. if j == len(vs) { - return vs[:i] + return vs[:i:i] } // Reaching here, the elements under both cursors are out of @@ -69,7 +72,7 @@ func Partition[T any](vs []T, keep func(T) bool) []T { i++ j++ } - return vs[:i] + return vs[:i:i] } // Dedup rearranges the elements of vs in-place to deduplicate consecutive runs diff --git a/slice/slice_test.go b/slice/slice_test.go index 05b321c..8e7eb70 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -489,6 +489,14 @@ func (tc *testCase[T]) partition(t *testing.T) { if diff != "" { t.Errorf("Partition result (-want, +got)\n%s", diff) } + + // Verify that the output is clipped to its length. + cp2 := copyOf(cp) + var zero T + _ = append(got, zero) + if diff := cmp.Diff(cp, cp2); diff != "" { + t.Errorf("After append to result (-got, +want):\n%s", diff) + } } func copyOf[T any](vs []T) []T {