## Repeat blocks

Repeat blocks provide an efficient way to repeat part of a model many times. A repeat block takes an argument count and contains the statements which are to be repeated:

``````using (Variable.Repeat(count))
{
// contained modelling statements
}
``````

For an integer count, a repeat block is equivalent to a ForEach block, in the sense that it repeats the contained statements count times. However, a repeat block is much more efficient. It achieves this efficiency by not providing any index variable—every repetition must be identical. Also, local variables of a repeat block cannot be used outside of the block (otherwise they would have to be treated as arrays of length count, which we want to avoid). As with ForEach blocks, the using syntax is optional.

### Using repeat blocks to speed up inference

When parts of a model are repeated identically, repeat blocks can lead to substantial speed-ups. For example, suppose we wish to learn the probability of heads of a coin from 10,000,000 tosses. Let’s say we are given a bool array flips containing these coin tosses. The standard approach in Infer.NET would be:

``````var probHeads = Variable.Beta(1, 1); // The bias of the coin
var N = new Range(flips.Length); // Range over the flips
var flipArray = Variable.Array<bool>(N); // Array of flips
// Model for the flips
flipArray.ObservedValue = flips;
``````

Intuitively this seems very inefficient - we have to allocate and process large arrays. The code will take longer and longer as we make more and more coin tosses.

In this particular model, all tosses which give heads are equivalent, as are all tosses that give tails. So we can just record the counts of heads and tails instead of all individual coin tosses. Repeat blocks provide a consistent way to do this for any type of model.

Here is how we can write the same model using repeat blocks, assuming we now have counts of heads and tails in the variables countTrue and countFalse:

``````using(Variable.Repeat(countTrue)) {