Last active
August 29, 2015 14:07
-
-
Save louissmit/4ec16497e74bc07da012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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