Big Data/BI Zone is brought to you in partnership with:

A GNU/Linux g33k CLI+Web loving computer polyglot. Hemanth is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

A JavaScript MapReduce One-Liner

10.01.2012
| 2982 views |
  • submit to reddit
The Big Data/BI Zone is brought to you in partnership with Jaspersoft and GridGain.  Here you'll learn to work with large data sets, gain Business Intelligence,  and even brush up on your statistics and data science.  GridGain and Jaspersoft have leading experts and resources in the Big Data space.

After my post on "Word frequency using MapReduce in Python," I got my paws dirty with some silly Javascript. Once I reduced a whole chunk of code, it turned out to be a simple one-liner in JavaScript. A linear implementation of MapReduce, but it was fun and indeed will be useful if on node.js.

Enough talking! Below is the one liner that will give the word frequency in JSON format.

One-liner:

String.prototype.map_reduce = function(){return this.toLowerCase().split(/\W+/g).reduce(function (t, w) { if (w) t[w] = (t[w] || 0) + 1; return t; }, {}).toSource()}

The prettier version:

String.prototype.map_reduce = function () {
return this.toLowerCase().
split(/\W+/g).
reduce(function (t, w) {
if (w) {
t[w] = (t[w] || 0) + 1;
}
return t;
}, {}).
toSource()
}

Example :

>>> "The quick brown fox jumped over the lazy".map_reduce()

>>> "({the:2, quick:1, brown:1, fox:1, jumped:1, over:1, lazy:1, dog:1})"

 

Published at DZone with permission of Hemanth Madhavarao, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

The Big Data/BI Zone is a prime resource and community for Big Data geeks of all stripes.  We're on top of all the best tips and news for Hadoop, R, and data visualization technologies.  Not only that, but we also give you advice from data science experts on how to understand and present that data.  With great resources and experts from two leaders in the space, GridGain and Jaspersoft, we can bring you the most practical information for dealing with today's data challenges.

Comments

Thomas Eichberger replied on Thu, 2012/10/04 - 1:32am

Cool! :-)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.