Created
December 22, 2010 05:04
-
-
Save jkk/751118 to your computer and use it in GitHub Desktop.
karma.clj
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
;; re http://groups.google.com/group/clojure/browse_thread/thread/55faa29db75192d0 | |
(ns user | |
(use [clojure.java.io :only [reader]])) | |
(def re-vote #"([A-z]{1,16})(\+\+|\-\-)") | |
(defn extract-votes | |
[line] | |
(map rest (re-seq re-vote line))) | |
(defn reckon | |
[log] | |
(with-open [r (reader log)] | |
(let [votes (mapcat extract-votes (line-seq r))] | |
(reduce | |
(fn [tally [nick vote]] | |
(let [inc-or-dec (if (= vote "++") inc dec)] | |
(assoc tally nick (inc-or-dec (tally nick 0))))) | |
{} | |
votes)))) | |
(defn -main [& args] | |
(let [tally (->> (ffirst args) | |
reckon | |
(remove (comp zero? val)) | |
(sort-by (comp - val)))] | |
(doseq [[nick score] tally] | |
(println nick score)))) | |
(comment | |
(-main ["/Users/tin/src/clj/playground/karma.txt"]) | |
baz 4 | |
bar 3 | |
foo -2 | |
nil | |
) |
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
blah foo++ blah | |
bar++ blah blah blah | |
blah blah blah baz++ foo-- blah blah | |
blah bar++ blah blah bar++ | |
blah baz-- baz++ foo-- blah blah | |
foo-- | |
baz++ blah blah | |
blah blah baz++ | |
blah baz++ blah | |
quux++ quux-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment