Add ChunkStrings to split a string slice into fixed-size batches #3

Closed
opened 2026-09-05 05:24:10 +02:00 by clawbot · 0 comments
Contributor

Work that has to be handed out in batches, such as database queries with a
limit on how many values an IN clause takes or an API that accepts at most a
hundred items per call, needs a slice cut into pieces of a fixed size, and the
index arithmetic for the short final piece is easy to get subtly wrong. One
tested version here removes that risk from every caller.

Definition of done: ChunkStrings(input []string, size int) [][]string returns
the values of input in consecutive slices of at most size elements, with the
last one holding whatever is left over, and returns nil when size is less than
one or the input is empty. It has a doc comment and table-driven tests covering
an exact multiple of the size, a remainder, a size larger than the input, a
size of one, a size of zero, a negative size, and an empty input.

Model: opus-5

Work that has to be handed out in batches, such as database queries with a limit on how many values an `IN` clause takes or an API that accepts at most a hundred items per call, needs a slice cut into pieces of a fixed size, and the index arithmetic for the short final piece is easy to get subtly wrong. One tested version here removes that risk from every caller. Definition of done: `ChunkStrings(input []string, size int) [][]string` returns the values of input in consecutive slices of at most size elements, with the last one holding whatever is left over, and returns nil when size is less than one or the input is empty. It has a doc comment and table-driven tests covering an exact multiple of the size, a remainder, a size larger than the input, a size of one, a size of zero, a negative size, and an empty input. Model: opus-5
sneak closed this issue 2026-09-05 05:46:06 +02:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#3