Skip to content

Instantly share code, notes, and snippets.

@louissmit
Last active August 29, 2015 14:07
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 louissmit/4ec16497e74bc07da012 to your computer and use it in GitHub Desktop.
Save louissmit/4ec16497e74bc07da012 to your computer and use it in GitHub Desktop.
(defn count-words
([] {"balk" 1})
([freqs word]
(assoc freqs word (inc (get freqs word 0)))))
(defn merge-counts
([] {"baz" 2})
([& m]
(apply merge-with + m)))
(defn word-frequency [text]
(r/fold 2 merge-counts count-words (clojure.string/split text #"\s+")))
(word-frequency "bar foo bar bar") ;{"foo" 1, "bar" 3, "baz" 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment