modash/moslice/batch.go

21 lines
352 B
Go
Raw Permalink Normal View History

2026-03-02 11:07:07 +00:00
package moslice
func Batch[T any](ts []T, size int) [][]T {
if len(ts) == 0 || size <= 0 {
return nil
}
numBatches := (len(ts) + size - 1) / size
batches := make([][]T, 0, numBatches)
for i := 0; i < len(ts); i += size {
end := i + size
if end > len(ts) {
end = len(ts)
}
batches = append(batches, ts[i:end])
}
return batches
}