aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-08 13:27:25 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-08 13:27:25 -0700
commitf5e4a7bc97c2642cf5cc3650a062c37bb8b4dbc7 (patch)
treeac2d264af5241ac9238245c59cb33efe4a2c8172
parent01d318f579ad3fdae1b796d57e83daccbe2c4198 (diff)
downloadperlweeklychallenge-club-f5e4a7bc97c2642cf5cc3650a062c37bb8b4dbc7.tar.gz
perlweeklychallenge-club-f5e4a7bc97c2642cf5cc3650a062c37bb8b4dbc7.tar.bz2
perlweeklychallenge-club-f5e4a7bc97c2642cf5cc3650a062c37bb8b4dbc7.zip
Ch81/Task 2: optimize and add comments
We just need a count of the words, which can be achieved by summing (via x/reduce +) 1 for every instance we find. Add comments to clarify what the algorithm is doing at each step.
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj8
1 files changed, 4 insertions, 4 deletions
diff --git a/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj
index 543a1f54b6..c126e053b3 100644
--- a/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj
@@ -18,10 +18,10 @@
[source]
(let [cleaner (fn [s] (str/replace s #"(?:[.\"\(\),]|'s|--|\n)" " "))
splitter (fn [s] (str/split s #" "))
- xf (comp (mapcat (comp splitter cleaner))
- (remove #{""})
- (x/by-key identity (x/into []))
- (x/by-key (comp count second) first (x/into (sorted-set))))]
+ xf (comp (mapcat (comp splitter cleaner)) ; wordify
+ (remove #{""}) ; discard empty strings
+ (x/by-key identity (constantly 1) (x/reduce +)) ; hash (word => count)
+ (x/by-key second first (x/into (sorted-set))))] ; invert hash, combine words
(into (sorted-map) xf source)))
(defn -main