Last active
November 15, 2017 22:22
-
-
Save sundbp/fd27156e94f363796c7eb3a6ea4e02d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ;;; leaderboard | |
| (defn parse-goal-csv | |
| [filename] | |
| (->> (drop 1 (csv/read-csv (slurp filename))) | |
| (map (fn [r] | |
| {:season (nth r 0) | |
| :league_id (to-int (nth r 1)) | |
| :date (nth r 2) | |
| :match_id (to-int (nth r 3)) | |
| :home_team_id (to-int (nth r 4)) | |
| :home_team_name (nth r 5) | |
| :away_team_id (to-int (nth r 6)) | |
| :away_team_name (nth r 7) | |
| :goal_scorer (to-int (nth r 8)) | |
| :scoring_team (to-int (nth r 9)) | |
| :goal_time (to-int (nth r 10))})))) | |
| (defn goals-by-player | |
| [goals] | |
| (reduce (fn [ret g] | |
| (let [existing (get ret (:goal_scorer g) 0)] | |
| (assoc ret (:goal_scorer g) (inc existing)))) | |
| {} | |
| goals)) | |
| (defn get-player-lookup | |
| [filename] | |
| (->> (csv/read-csv (slurp filename)) | |
| (drop 1) | |
| (reduce (fn [ret r] | |
| (assoc ret | |
| (to-int (nth r 1)) | |
| (nth r 2))) | |
| {}))) | |
| (defn leaderboard | |
| [] | |
| (let [players (get-player-lookup "/Users/sundbp/Downloads/soccer/players.csv") | |
| by-player (->> (parse-goal-csv "/Users/sundbp/Downloads/soccer/my-goals.csv") | |
| goals-by-player | |
| (sort-by second >) | |
| (map (fn [entry] | |
| {:player (get players (first entry)) | |
| :goals (second entry)})))] | |
| (clojure.pprint/print-table [:player :goals] (take 10 by-player)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment