Add ChunkStrings to split a string slice into fixed-size batches #3
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Work that has to be handed out in batches, such as database queries with a
limit on how many values an
INclause takes or an API that accepts at most ahundred 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) [][]stringreturnsthe 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