Skip to content

Instantly share code, notes, and snippets.

@ApricotLace
Created May 27, 2020 00:28
Show Gist options
  • Save ApricotLace/11f10f1be1b0f465bfb0052675c211e7 to your computer and use it in GitHub Desktop.
Save ApricotLace/11f10f1be1b0f465bfb0052675c211e7 to your computer and use it in GitHub Desktop.
(ns future-atoms-task.core)
(defn filter-all-non-alphabet-chars
[text]
(clojure.string/join (filter (fn [char]
(or (Character/isWhitespace char) (Character/isLetter char)))
text)))
(defn count-words
[text]
(reduce (fn [acc word]
(if (contains? acc word)
(update acc word inc)
(assoc acc word 1)))
{}
(clojure.string/split text #" ")))
(defn trim-author-name
[quote-text]
(println quote-text)
(let [newline-index (clojure.string/index-of quote-text "\n")]
(subs quote-text 0 newline-index)))
(def quotes-resource "https://www.braveclojure.com/random-quote")
(defn push-new-state
[new-state counted-words]
(swap! counted-words #(merge-with + % new-state)))
(defn quote-word-count
[quotes-amount]
(let [counted-words (atom {})
futures
(doall (repeatedly quotes-amount #(future (-> (slurp quotes-resource)
trim-author-name
clojure.string/lower-case
filter-all-non-alphabet-chars
count-words
(push-new-state counted-words)))))]
(doall (map deref futures))
counted-words))
(deref (quote-word-count 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment