Skip to content

Instantly share code, notes, and snippets.

@tthew
Last active December 11, 2015 20:28
Show Gist options
  • Select an option

  • Save tthew/4655406 to your computer and use it in GitHub Desktop.

Select an option

Save tthew/4655406 to your computer and use it in GitHub Desktop.
An implementation of Map Reduce in Javascript
// map_reduce, based on http://code.google.com/p/mapreduce-js/
var map_reduce = function (data, map, reduce) {
var mapResult = [], reduceResult = [];
var mapIx, reduceKey;
var mapEmit = function(key, value) {
if (!mapResult[key]) {
mapResult[key] = [];
}
mapResult[key].push(value);
};
var reduceEmit = function (obj) {
reduceResult.push(obj);
};
for (mapIx = 0; mapIx < data.length; mapIx++) {
map(data[mapIx], mapEmit);
}
for (reduceKey in mapResult) {
reduce(reduceKey, mapResult[reduceKey], reduceEmit);
}
return reduceResult;
};
// Example
// -------
// Source String
var lorum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eget purus purus, in accumsan urna. Cras magna turpis, aliquet vitae consequat et, fringilla volutpat nibh. Phasellus mollis sodales risus vel scelerisque. Nunc at fringilla turpis. Duis tempus eleifend condimentum. In dapibus porttitor tortor a iaculis. Proin sit amet lorem turpis, in pharetra dolor. Nullam eu turpis eu turpis ultrices dapibus sed in leo. Fusce tortor sapien, tempus id bibendum non, fringilla nec tortor. Proin tincidunt justo quis magna suscipit posuere. Mauris sed tellus neque. Aenean tincidunt mauris in nisi faucibus volutpat. Ut auctor ipsum vel neque ornare ac viverra libero euismod. Proin et mauris eu sapien faucibus gravida. Mauris ac nibh sem. Maecenas eu dolor velit.";
var ipsum = "Donec ac ipsum leo. Praesent enim felis, commodo sed vulputate ut, rutrum quis diam. Vestibulum tellus mi, consectetur at dapibus in, lobortis id dui. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi fringilla metus facilisis tellus accumsan posuere suscipit ultrices erat. Integer sit amet tellus mauris, eget dignissim lectus. Etiam non erat leo, a luctus mauris. Praesent varius luctus bibendum. Donec leo sapien, convallis vitae ornare nec, semper vitae ipsum. Pellentesque a mi felis, a bibendum velit. Mauris consequat tempus sem eget consequat.";
// this demo counts the number of words in a string for an array of strings
var data = [ lorum, ipsum ];
// map_reduce (inputSet, mapFunction, reduceFunction)
var result = map_reduce (data, function(item, emit) {
// the map function takes an item from the data-set
// and can map this to a set of new items
var words = item.toLowerCase().split(/\s/g);
for(var word in words) {
// the 'emit' function is used to yield the new items
// syntax: emit (key, value);
emit(words[word], 1);
}
}, function(key, values, emit) {
// the reduce function retrieves the emitted items
// by key. The values that were emitted are grouped by key, and are in the 'values' array.
// the emit function is used to return the results
// syntax: emit (value)
emit({ key: key, count: values.length });
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment