# reduce¶

Performs reduction operations on `Queue` data like calculating a sum of all `Queue` elements, or like calculating XOR checksum.

`reduce`(din: Queue, init, *, f) → init

Calculates a reduction of an input `Queue`, using the binary operation `f` and the initial value `init`. The output data type, as well as the internal register, are the same as the type of the `init` input.

The example shows how to calculate XOR checksum of the input `Queue`. The input `init` has been fixed to `0`, but has also been given a data type `Uint[8](0)` to make sure that internal register width and output value is also `Uint[8]`

```drv(t=Queue[Uint[8]], seq=[[0xff, 0xff, 0xff, 0xff]]) \
| reduce(init=Uint[8](0), f=lambda x, y: x ^ y) \
| check(ref=[0])
```

Next example shows how to pack bits received as a `Queue` into a parallel data of the type `Uint`. It also shows that `init` input can be supplied at run time.

```(drv(t=Queue[Uint[8]], seq=[[0, 1, 0, 1, 0, 1, 0]]),
drv(t=Uint[8], seq=[1])) \
| reduce(f=lambda x, y: (x << 1) | y) \
| check(ref=[0xaa])
```
`accum`(din: Queue[Integer], init: Integer) → init

The `accum()` gear is a convenience gear for calculating the sum of all elements of the input `Queue`. It relies on the `reduce()` gear underneath:

```@gear
def accum(din: Queue[Integer], init: Integer) -> b'init':
return reduce(din, init, f=lambda x, y: x + y)
```
```drv(t=Queue[Uint[4]], seq=[[0, 1, 2, 3, 4]]) \
| accum(init=Uint[8](0)) \
| check(ref=[10])
```