Skip to content

Instantly share code, notes, and snippets.

@shamod
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamod/4362a6eec0b092769b3c to your computer and use it in GitHub Desktop.
Save shamod/4362a6eec0b092769b3c to your computer and use it in GitHub Desktop.
WordCount - Functional
var paragraph = 'How much wood would a woodchuck chuck\nIf a woodchuck could chuck wood?\nHe would chuck, he would, as much as he could,\nAnd chuck as much as a woodchuck would\nIf a woodchuck could chuck wood.';
var paraCount = paragraph.split('\n')
.reduce(joinArray)
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.reduce(wordCount, {});
function joinArray(last, now) {
return last.toLowerCase().concat(' ' + now.toLowerCase());
}
function wordCount(map, word) {
map[word] = (map[word] || 0) + 1;
return map;
}
console.log(paraCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment