Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Last active December 22, 2015 00:48
Show Gist options
  • Save pbostrom/6391396 to your computer and use it in GitHub Desktop.
Save pbostrom/6391396 to your computer and use it in GitHub Desktop.
Football rankings
(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}}
@ulsa
Copy link

ulsa commented Sep 4, 2013

The compare function can be used together with case to get less repetition (for the price of less clarity, I guess):

(defn process-match [ts match]
  (let [{:keys [home away home_score away_score]} match]
    (case (compare home_score away_score)
     1 (update-in ts [home :points] + 3)
     -1 (update-in ts [away :points] + 3)
     (-> ts (update-in [home :points] + 1) (update-in [away :points] + 1)))))

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