Skip to content

Instantly share code, notes, and snippets.

@padurean
Created November 26, 2019 17:36
Show Gist options
  • Save padurean/86961c14c7250efc361951c04fdb2e0a to your computer and use it in GitHub Desktop.
Save padurean/86961c14c7250efc361951c04fdb2e0a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func makeBatches(batchSize int, size int) [][]int {
batches := [][]int{}
nbBatches := int(math.Ceil(float64(size) / float64(batchSize)))
for b := 0; b < nbBatches; b++ {
start := b * batchSize
end := (b+1) * batchSize
if size < end {
end = size
}
batches = append(batches, []int{start, end})
}
// fmt.Printf("%+v\n", batches)
return batches
}
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}
func main() {
a := makeRange(1, 23)
fmt.Println(a)
for bi, batch := range makeBatches(10, len(a)) {
fmt.Printf("batch %d (length %d)\n", bi, batch[1] - batch[0])
fmt.Printf(" %+v\n", makeRange(batch[0], batch[1]))
fmt.Printf(" %+v\n", a[batch[0]:batch[1]])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment