Skip to content

Instantly share code, notes, and snippets.

@jkk
Created December 22, 2010 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkk/751118 to your computer and use it in GitHub Desktop.
Save jkk/751118 to your computer and use it in GitHub Desktop.
karma.clj
;; 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
)
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