SortStableFunc(s, cmp)
return s
}
+
+// Chunk returns an iterator over consecutive sub-slices of up to n elements of s.
+// All but the last sub-slice will have size n.
+// All sub-slices are clipped to have no capacity beyond the length.
+// If s is empty, the sequence is empty: there is no empty slice in the sequence.
+// Chunk panics if n is less than 1.
+func Chunk[Slice ~[]E, E any](s Slice, n int) iter.Seq[Slice] {
+ if n < 1 {
+ panic("cannot be less than 1")
+ }
+
+ return func(yield func(Slice) bool) {
+ for i := 0; i < len(s); i += n {
+ // Clamp the last chunk to the slice bound as necessary.
+ end := min(n, len(s[i:]))
+
+ // Set the capacity of each chunk so that appending to a chunk does
+ // not modify the original slice.
+ if !yield(s[i : i+end : i+end]) {
+ return
+ }
+ }
+ }
+}
t.Errorf("SortedStableFunc wasn't stable on %d reverse ints", n)
}
}
+
+func TestChunk(t *testing.T) {
+ cases := []struct {
+ name string
+ s []int
+ n int
+ chunks [][]int
+ }{
+ {
+ name: "nil",
+ s: nil,
+ n: 1,
+ chunks: nil,
+ },
+ {
+ name: "empty",
+ s: []int{},
+ n: 1,
+ chunks: nil,
+ },
+ {
+ name: "short",
+ s: []int{1, 2},
+ n: 3,
+ chunks: [][]int{{1, 2}},
+ },
+ {
+ name: "one",
+ s: []int{1, 2},
+ n: 2,
+ chunks: [][]int{{1, 2}},
+ },
+ {
+ name: "even",
+ s: []int{1, 2, 3, 4},
+ n: 2,
+ chunks: [][]int{{1, 2}, {3, 4}},
+ },
+ {
+ name: "odd",
+ s: []int{1, 2, 3, 4, 5},
+ n: 2,
+ chunks: [][]int{{1, 2}, {3, 4}, {5}},
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ var chunks [][]int
+ for c := range Chunk(tc.s, tc.n) {
+ chunks = append(chunks, c)
+ }
+
+ if !chunkEqual(chunks, tc.chunks) {
+ t.Errorf("Chunk(%v, %d) = %v, want %v", tc.s, tc.n, chunks, tc.chunks)
+ }
+
+ if len(chunks) == 0 {
+ return
+ }
+
+ // Verify that appending to the end of the first chunk does not
+ // clobber the beginning of the next chunk.
+ s := Clone(tc.s)
+ chunks[0] = append(chunks[0], -1)
+ if !Equal(s, tc.s) {
+ t.Errorf("slice was clobbered: %v, want %v", s, tc.s)
+ }
+ })
+ }
+}
+
+func TestChunkPanics(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ x []struct{}
+ n int
+ }{
+ {
+ name: "cannot be less than 1",
+ x: make([]struct{}, 0),
+ n: 0,
+ },
+ } {
+ if !panics(func() { _ = Chunk(test.x, test.n) }) {
+ t.Errorf("Chunk %s: got no panic, want panic", test.name)
+ }
+ }
+}
+
+func TestChunkRange(t *testing.T) {
+ // Verify Chunk iteration can be stopped.
+ var got [][]int
+ for c := range Chunk([]int{1, 2, 3, 4, -100}, 2) {
+ if len(got) == 2 {
+ // Found enough values, break early.
+ break
+ }
+
+ got = append(got, c)
+ }
+
+ if want := [][]int{{1, 2}, {3, 4}}; !chunkEqual(got, want) {
+ t.Errorf("Chunk iteration did not stop, got %v, want %v", got, want)
+ }
+}
+
+func chunkEqual[Slice ~[]E, E comparable](s1, s2 []Slice) bool {
+ return EqualFunc(s1, s2, Equal[Slice])
+}