We need a data structure to hold players. Use a map that maps directly to the data format. Write a function that, given a line of data, returns a player.
(defn player [data]
"Returns a player bursted out of `data`."
(let [[id lname fname bats throws team-id position]
(clojure.string/split data #",")]
{:id id :lname lname :fname fname :bats bats :throws throws
:team-id team-id :position position}))
(defn players-for-lines [lines]
"Returns a list of players created from `lines`."
(if (empty? lines)
[]
(conj (players-for-lines (rest lines))
(player (clojure.string/trim (first lines))))))
(defn load-players [file]
"Returns a roster of players loaded from file `file`."
(players-from-lines (clojure.string/split (slurp file)
#"\n")))
(def pirates (load-players (str data-directory "PIT2013.ROS")))
(count pirates)
49
(first pirates)
(def pirates-pitchers (filter #(= (:position %1) "P") pirates))
(pprint (last pirates-pitchers))
…
(def southpaws (filter #(= (:throws %1) "L") pirates-pitchers))
(def righties (filter #(= (:throws %1) "R") pirates-pitchers))
(/ (count southpaws) (count righties))
8/19
(:bats (first (filter #(and (= (:fname %1) "Francisco")
(= (:lname %1) "Liriano")) pirates-pitchers)))
“L”
ANA,A,Anaheim,AngelsEach line in the file represents one team, so write a function that takes a line as input and returns a team.
(defn team [data]
"Returns a team map bursted out of `data`."
(let [[id league city name] (clojure.string/split data #",")]
{:id id :league league :city city :name name}))
There are a number of teams in one file, one per line, so write a function that takes a list of lines and returns a list of teams.
(defn teams-for-lines [lines]
"Returns a map of teams created from `lines`, indexed by id."
(if (empty? lines)
{}
(let [this-team (team (clojure.string/trim (first lines)))
this-team-id (:id this-team)]
(conj (teams-for-lines (rest lines))
(hash-map this-team-id this-team)))))
(defn load-teams [file]
"Returns teams loaded from file `file`".
(teams-for-lines (clojure.string/split (slurp file)
#"\n")))
(def teams (load-teams "TEAMS2013"))
(keys teams)
(“COL” “NYN” “MIA” “BOS” “TEX” “CIN” “CHN” “KCA” “WAS” “BAL” “HOU” “SEA” “MIL” “PHI” “MIN” “TBA” “DET” “ANA” “SLN” “NYA” “TOR” “ARI” “OAK” “LAN” “ATL” “SFN” “PIT” “CHA” “CLE” “SDN”)
#+name: testing-load-teams-get-pirates
(get teams "PIT")
{:id “PIT”, :league “N”, :city “Pittsburgh”, :name “Pirates”}
#+name: load-roster
(defn load-roster [team players]
(filter #(= (:team-id %1) team.id) pirates))