Implementing Batching

There are some tools we keep in our engineering toolbox which are extremely helpful in various situations but can be a little tricky to get our heads around initially. Let’s take a look at something which is pretty simple, but just needs a little practice sometimes to get the hang of: batching.

In a quick nutshell, here’s the idea: we have a batch size, say fifty and we have a lower bound, 1, and an upper bound, 1000. Given this range, we should expect to have twenty batches of fifty since \(20 \cdot 50 = 1000\). That’s straight forward enough, but how do we implement this?

If the range (the upper bound minus the lower bound) can be divided by the batch size, then all of our batches will be the same size. However, when the bounds are dynamic, we need to account for the batches with our pre-defined size as well as the overflow.

\[\text{number of batches} = \frac{\text{upper bound} - \text{lower bound} + 1}{\text{batch size}}\]
upper_bound - lower_bound < batch_size && return throw(BoundsError)
iterations = upper_bound / batch_size

if upper_bound % batch_size != 0
    iterations -= 1
end

for k in 0:iterations
    println((k * batch_size + 1), ": ", ((k+1) * batch_size))    
end