Skip to content

Instantly share code, notes, and snippets.

@esmevane
Created March 19, 2015 15:01
Show Gist options
  • Select an option

  • Save esmevane/674caa23599f3ecf9966 to your computer and use it in GitHub Desktop.

Select an option

Save esmevane/674caa23599f3ecf9966 to your computer and use it in GitHub Desktop.
[ Ruby / Map Reduce ]: Count words, quick / naive
none_shall_pass = """
Flash that buttery gold, jittery zeitgeist
Wither by the watering hole, Border patrol
What are we to Heart Huckabee
Art fuckery suddenly
Not enough young in his lung for the water wings
Colorfully vulgar poacher, out of mulch
Like I'm a pull the pulse out a soldier and bolt
Fine, sign of the time we elapse
When a primate climb up a spine and attach
Eye for an eye, by the bog life swamps and vines
They get a rise out of frogs and flies
So when a dogfight's hog-tied prize sort of costs a life
The mouths water on a fork and knife
And the allure isn't right
No score on a war-torn beach
Where the cash cow's actually beef
Blood turns wine when it leak for police
Like that's not a riot it's a feast, let's eat
"""
word_counts = none_shall_pass.split.map do |word|
word = word.downcase.to_sym
{ word: word, value: 1 }
end
dictionary = word_counts.reduce({}) do |hash, word_count|
key = word_count.fetch(:word)
value = word_count.fetch(:value, 0)
hash[key] ||= 0
hash[key] += value
hash
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment