Added moslice.Batch
All checks were successful
ci / deploy (push) Successful in 54s

This commit is contained in:
Leon Mika 2026-03-02 22:07:07 +11:00
parent cdaa1c8abb
commit 31c6b125c9
2 changed files with 72 additions and 0 deletions

20
moslice/batch.go Normal file
View file

@ -0,0 +1,20 @@
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
}