Created
September 19, 2010 18:53
-
-
Save aria42/587009 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
(defrecord Counter [counts total]) | |
(defn get-count | |
"retrieve count of k from counter, should not be negative" | |
[counter k] | |
{:post [(not (neg? %))]} | |
(get (:counts counter) k 0.0)) | |
(defn inc-count | |
"increment-count of k in counter by weight amount" | |
[counter k weight] | |
(let [new-count (+ (get-count counter k) weight)] | |
(Counter. (if (zero? new-count) | |
(dissoc (:counts counter) k) | |
(assoc (:counts counter) k new-count)) | |
(+ (:total counter) weight)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment