reduce()

Wraps reducer providing data streaming as well as value reduction during the stream.

class RunOpenCode\Component\Dataset\Operator\Reduce
__construct(iterable<TKey, TValue, ReducerInterface<TKey, TValue, TReducedValue>> $source
Parameters:
  • $source – iterable<TKey, TValue> Stream source to iterate over.

getIterator()
Returns:

\Traversable<TKey, TReducedValue> Stream with original keys and values.

Use cases

  • Reduce keys/values while streaming through iterable.

Example

Calculate sum of values.

1<?php
2
3use RunOpenCode\Component\Dataset\Stream;
4
5new Stream([1, 2, 3, 4, 5])
6    ->reduce($total = new Count())
7    ->flush();
8
9echo $total->value; // 15