Created
April 25, 2018 08:18
-
-
Save kajstrom/6a8b1124cf37c918fffd254ee3eb935a to your computer and use it in GitHub Desktop.
Counting words for quotes in parallel
This file contains hidden or 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
(ns clojure-noob.meta-exercises | |
(:gen-class)) | |
(def word-count (atom {})) | |
(defn set-words [m words] | |
(reduce (fn [m word] | |
(if (contains? m word) | |
(update-in m [word] inc) | |
(assoc m word 1))) | |
m | |
words)) | |
(defn split-words | |
[s] | |
(->> s | |
(#(clojure.string/split % #" ")) | |
(map clojure.string/trim) | |
(map clojure.string/trim-newline))) | |
(defn get-quote | |
[] | |
(println "Getting quote...") | |
(future (let [quote (slurp "https://www.braveclojure.com/random-quote")] | |
(let [words (split-words quote)] | |
(swap! word-count set-words words))))) | |
(defn quote-word-count | |
[cnt] | |
(let [calls (repeatedly cnt get-quote)] | |
(doseq [call calls] | |
(deref call 1000 :timeout))) | |
@word-count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment