Skip to content

Instantly share code, notes, and snippets.

@kajstrom
Created April 25, 2018 08:18
Show Gist options
  • Save kajstrom/6a8b1124cf37c918fffd254ee3eb935a to your computer and use it in GitHub Desktop.
Save kajstrom/6a8b1124cf37c918fffd254ee3eb935a to your computer and use it in GitHub Desktop.
Counting words for quotes in parallel
(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