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

Merged
sneak merged 1 commits from clawbot/util:proposal-chunk-strings into master 2026-09-05 05:46:04 +02:00
Contributor

Adds ChunkStrings(input []string, size int) [][]string, which returns the
values of the input in consecutive slices of at most size elements. The last
slice holds the remainder. A size below one, or an empty input, returns nil.

This belongs here because handing work out in batches is a constant need, from
splitting a list for an API that accepts a limited number of items per call to
paging through a set of identifiers, and the standard library of this module's
Go version has nothing for it. The arithmetic for the short final piece is
small but easy to get wrong once per project.

Things to know:

  • Each returned slice is a copy rather than a view into the input. Slicing the
    input in place would be cheaper, but then appending to one piece could
    silently overwrite the start of the next one, which is a nasty surprise.
  • The code is in a new file, chunk.go, rather than appended to util.go, so
    that the ten proposal branches do not all conflict in the same place.
  • make test on this branch reports one failure, TestNowUnixMicro. That test
    already fails on master and is unrelated to this change.

Model: opus-5

Adds `ChunkStrings(input []string, size int) [][]string`, which returns the values of the input in consecutive slices of at most `size` elements. The last slice holds the remainder. A size below one, or an empty input, returns nil. This belongs here because handing work out in batches is a constant need, from splitting a list for an API that accepts a limited number of items per call to paging through a set of identifiers, and the standard library of this module's Go version has nothing for it. The arithmetic for the short final piece is small but easy to get wrong once per project. Things to know: - Each returned slice is a copy rather than a view into the input. Slicing the input in place would be cheaper, but then appending to one piece could silently overwrite the start of the next one, which is a nasty surprise. - The code is in a new file, `chunk.go`, rather than appended to `util.go`, so that the ten proposal branches do not all conflict in the same place. - `make test` on this branch reports one failure, `TestNowUnixMicro`. That test already fails on `master` and is unrelated to this change. Model: opus-5
clawbot self-assigned this 2026-09-05 05:24:33 +02:00
clawbot added 1 commit 2026-09-05 05:24:34 +02:00
ChunkStrings cuts a slice into consecutive pieces of at most the requested
size, with the leftovers in the last piece, and returns copies so the pieces
cannot disturb each other. Comes with a doc comment and table-driven tests.
(closes #3)

Model: opus-5
sneak merged commit 2d714eea9f into master 2026-09-05 05:46:04 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#4