Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Forked from christianromney/core.clj
Created October 17, 2012 02:22
Show Gist options
  • Save ddeaguiar/3903367 to your computer and use it in GitHub Desktop.
Save ddeaguiar/3903367 to your computer and use it in GitHub Desktop.
Simple functions to calculate weighted averages in Clojure
(ns rubric.core)
(defn average
"Averages a set of scores"
[& scores]
(/ (apply + scores) (count scores)))
(defn weighted-average
"Calculates a total score from individual weighted results.
Expects pairs of <weight>, <score> whose weights add to 1.0."
[& args]
{:pre [(-> args count even?)
(= 1N (->> args (partition 2) (map first) (apply +)))]}
(->> args (partition 2) (map #(apply * %)) (apply +)))
(defn- main
"A quick test of the weighted-average calculation"
[& args]
(println (weighted-average 4/10 (average 96 93.7 97.2 96.4 94.8)
3/10 50
2/10 80
1/10 75)))
@ddeaguiar
Copy link
Author

The similarities between lines 13 and 14 imply that refactoring is in order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment