Created
May 27, 2020 00:28
-
-
Save ApricotLace/11f10f1be1b0f465bfb0052675c211e7 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
(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