Skip to content
Permalink
Browse files

Changing params for callbacks in MapReduce to they are consistent with

the rest of the functions in Collection
  • Loading branch information...
lorenzo committed Dec 31, 2013
1 parent 7db6888 commit edd811b9b9dcd82c6e5e3fe8c7367c3ea12e7028
@@ -494,11 +494,11 @@ public function indexBy($callback) {
public function countBy($callback) {
$callback = $this->_propertyExtractor($callback);
$mapper = function($key, $value, $mr) use ($callback) {
$mr->emitIntermediate($callback($value), $value);
$mapper = function($value, $key, $mr) use ($callback) {
$mr->emitIntermediate($value, $callback($value));
};
$reducer = function ($key, $values, $mr) {
$reducer = function ($values, $key, $mr) {
$mr->emit(count($values), $key);
};
return new Collection(new MapReduce($this, $mapper, $reducer));
@@ -84,12 +84,12 @@ class MapReduce implements IteratorAggregate {
*
* {{{
* $data = new \ArrayObject([1, 2, 3, 4, 5, 3]);
* $mapper = function ($key, $value, $mr) {
* $mapper = function ($value, $key, $mr) {
* $type = ($value % 2 === 0) ? 'even' : 'odd';
* $mr->emitIntermediate($type, $value);
* $mr->emitIntermediate($value, $type);
* };
*
* $reducer = function ($type, $numbers, $mr) {
* $reducer = function ($numbers, $type, $mr) {
* $mr->emit(array_unique($numbers), $type);
* };
* $results = new MapReduce($data, $mapper, $reducer);
@@ -103,11 +103,12 @@ class MapReduce implements IteratorAggregate {
*
* @param \Traversable $data the original data to be processed
* @param callable $mapper the mapper callback. This function will receive 3 arguments.
* The first one is the current results key, second the current value and third is
* The first one is the current value, second the current results key and third is
* this class instance so you can call the result emitters.
* @param callable $reducer the reducer callback. This function will receive 3 arguments.
* The first one is a bucket name that was mapped before, second one is the list
* of values inside the bucket and third one is an instance of this class.
* The first one is the list of values inside a bucket, second one is the name
* of the bucket that was created during the mapping phase and third one is an
* instance of this class.
* @return void
*/
public function __construct(\Traversable $data, callable $mapper, callable $reducer = null) {
@@ -133,11 +134,11 @@ public function getIterator() {
* Appends a new record to the bucket labelled with $key, usually as a result
* of mapping a single record from the original data.
*
* @param string $bucket the name of the bucket where to put the record
* @param mixed $value the record itself to store in the bucket
* @param string $bucket the name of the bucket where to put the record
* @return void
*/
public function emitIntermediate($bucket, $value) {
public function emitIntermediate($value, $bucket) {
$this->_intermediate[$bucket][] = $value;
}
@@ -166,7 +167,7 @@ public function emit($value, $key = null) {
protected function _execute() {
$mapper = $this->_mapper;
foreach ($this->_data as $key => $value) {
$mapper($key, $value, $this);
$mapper($value, $key, $this);
}
$this->_data = null;
@@ -176,7 +177,7 @@ protected function _execute() {
$reducer = $this->_reducer;
foreach ($this->_intermediate as $key => $list) {
$reducer($key, $list, $this);
$reducer($list, $key, $this);
}
$this->_intermediate = [];
$this->_executed = true;
@@ -35,13 +35,13 @@ public function testInvertedIndexCreation() {
'document_2' => 'History is not only amazing but boring',
'document_3' => 'One thing that is not boring is dogs'
];
$mapper = function($document, $row, $mr) {
$mapper = function($row, $document, $mr) {
$words = array_map('strtolower', explode(' ', $row));
foreach ($words as $word) {
$mr->emitIntermediate($word, $document);
$mr->emitIntermediate($document, $word);
}
};
$reducer = function($word, $documents, $mr) {
$reducer = function($documents, $word, $mr) {
$mr->emit(array_unique($documents), $word);
};
$results = new MapReduce(new ArrayIterator($data), $mapper, $reducer);
@@ -73,7 +73,7 @@ public function testInvertedIndexCreation() {
*/
public function testEmitFinalInMapper() {
$data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
$mapper = function ($key, $row, $mr) {
$mapper = function ($row, $key, $mr) {
foreach ($row as $number) {
$mr->emit($number);
}
@@ -91,7 +91,7 @@ public function testEmitFinalInMapper() {
*/
public function testReducerRequired() {
$data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
$mapper = function ($key, $row, $mr) {
$mapper = function ($row, $key, $mr) {
foreach ($row as $number) {
$mr->emitIntermediate('a', $number);
}

0 comments on commit edd811b

Please sign in to comment.
You can’t perform that action at this time.