Skip to content

Instantly share code, notes, and snippets.

@YarekTyshchenko
Created April 2, 2015 11:22
Show Gist options
  • Save YarekTyshchenko/df8c5c4f4bf54660e9ef to your computer and use it in GitHub Desktop.
Save YarekTyshchenko/df8c5c4f4bf54660e9ef to your computer and use it in GitHub Desktop.
Poor man's Hadoop in Javascript
var reduce = function(k, list) {
return [k, list];
};
var map = function(yield, item) {
yield(item[0], item[1]);
};
var PoorMansHadoop = function(map, reduce, data) {
var r = data.map(function(node) {
var mapperOutput = [];
var yield = function() {
return function(k, v) {
mapperOutput.push([k, v]);
}
}();
map(yield, node);
return mapperOutput;
}).reduce(function(list, nodeList) {
nodeList.forEach(function(node) {
if (list[node[0]]) {
list[node[0]].push(node[1]);
} else {
list[node[0]] = [node[1]];
}
});
return list;
}, {});
var output = [];
for (var key in r) {
if (r.hasOwnProperty(key)) {
output.push(reduce(key, r[key]));
}
}
return output;
}
// Example
var result = PoorMansHadoop(function(y, i) {
y(i, 1);
}, function(k, list) {
return {
key:k,
value:list.reduce(function(a, b){
return a + b;
}, 0)
};
}, ['foo', 'bar', 'apple', 'foo', 'banana', 'foo', 'foo', 'bar']);
@auscompgeek
Copy link

Careful there, yield is a reserved keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment