Skip to content

Instantly share code, notes, and snippets.

@sylvainfilteau
Created March 9, 2012 22:09
Show Gist options
  • Save sylvainfilteau/2008981 to your computer and use it in GitHub Desktop.
Save sylvainfilteau/2008981 to your computer and use it in GitHub Desktop.
PHP map reduce (I hate myself)
<?php
$partition = function ($mapper, array $data, $nb_batches = 2) {
$length = count($data);
$nb_chunk = ceil($length / $nb_batches);
$chunks = array_chunk($data, $nb_chunk);
$batches = array();
foreach ($chunks as $chunk) {
$group = array();
array_map(function($to_map) use ($mapper, &$group) {
list($k, $v) = $mapper($to_map);
if (!isset($group[$k])) {
$group[$k] = array();
}
$group[$k][] = $v;
}, $chunk);
$batches[] = $group;
}
return $batches;
};
$reduce_batches = function($reducer, $batches) {
$i = 0;
$new_batches = array();
foreach ($batches as $batch) {
$new_batch = array();
foreach ($batch as $k => $v) {
$new_batch[$k] = array($reducer($k, $v));
}
if ($i++ % 2 != 0) {
$last_batch = count($new_batches) - 1;
$new_batches[$last_batch] = array_merge_recursive($new_batches[$last_batch], $new_batch);
} else {
$new_batches[] = $new_batch;
}
}
return $new_batches;
};
$map_reduce = function(array $data, $mapper, $reducer, $nb_batches = 2) use ($partition, $reduce_batches) {
$batches = $partition($mapper, $data, $nb_batches);
$result = array();
do {
$batches = $reduce_batches($reducer, $batches);
} while(count($batches) > 1);
$batches = $reduce_batches($reducer, $batches);
return $batches[0];
};
<?php
/**
* USAGE: php num_char.php your string here
*/
require_once 'mapreduce.php';
array_shift($argv);
$chars = str_split(implode(' ', $argv));
$map = function($char) {
return array($char, 1);
};
$reduce = function($key, $values) {
return array_sum($values);
};
var_dump($map_reduce($chars, $map, $reduce));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment