Implements a simplistic version of the popular Map-Reduce algorithm. Acts like an iterator for the original passed data after each result has been processed, thus offering a transparent wrapper for results coming from any source.
__construct( Traversable $data , callable $mapper , callable $reducer null )
Constructor
Separate all unique odd and even numbers in an array
$data = new \ArrayObject([1, 2, 3, 4, 5, 3]); $mapper = function ($value, $key, $mr) { $type = ($value % 2 === 0) ? 'even' : 'odd'; $mr->emitIntermediate($value, $type); }; $reducer = function ($numbers, $type, $mr) { $mr->emit(array_unique($numbers), $type); }; $results = new MapReduce($data, $mapper, $reducer);
Previous example will generate the following result:
['odd' => [1, 3, 5], 'even' => [2, 4]]
$data
$mapper
$reducer
optional null _execute( )
Runs the actual Map-Reduce algorithm. This is iterate the original data and call the mapper function for each , then for each intermediate bucket created during the Map phase call the reduce function.
LogicException
emit( mixed $value , string $key null )
Appends a new record to the final list of results and optionally assign a key for this record.
$value
$key
optional null emitIntermediate( mixed $value , string $bucket )
Appends a new record to the bucket labelled with $key, usually as a result of mapping a single record from the original data.
$value
$bucket
getIterator( )
Returns an iterator with the end result of running the Map and Reduce phases on the original data
ArrayIterator
IteratorAggregate::getIterator()
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/3.1/class-Cake.Collection.Iterator.MapReduce.html