Last active
December 22, 2015 00:48
-
-
Save pbostrom/6391396 to your computer and use it in GitHub Desktop.
Football rankings
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
(def m | |
[{:home "Manchester United" :away "Manchester City" :home_score 1 :away_score 0} | |
{:home "Manchester United" :away "Manchester City" :home_score 2 :away_score 0} | |
{:home "Manchester City" :away "Arsenal" :home_score 1 :away_score 1}]) | |
(def teams | |
{"Manchester United" {:points 1200} | |
"Manchester City" {:points 1200} | |
"Arsenal" {:points 1200}}) | |
(defn process-match [ts match] | |
(let [{:keys [home away home_score away_score]} match] | |
(cond | |
(> home_score away_score) (update-in ts [home :points] + 3) | |
(> away_score home_score) (update-in ts [away :points] + 3) | |
(= home_score away_score) (-> ts | |
(update-in [home :points] + 1) | |
(update-in [away :points] + 1))))) | |
> (reduce process-match teams m) | |
{"Manchester United" {:points 1206}, "Arsenal" {:points 1201}, "Manchester City" {:points 1201}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
compare
function can be used together withcase
to get less repetition (for the price of less clarity, I guess):