Skip to content

Instantly share code, notes, and snippets.

@mmower
Created September 4, 2013 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmower/b755a0b6f88292d9cec2 to your computer and use it in GitHub Desktop.
Save mmower/b755a0b6f88292d9cec2 to your computer and use it in GitHub Desktop.
Parse a CSV file containing rows like "2008,08,1.8,45454,8900,coupe" into a map that looks like: { "2008/1.8/coupe" [ {:mileage 45454 :price 8900}, ... ] ... } that can be further used to extract summary stats.
(defn group-car-data-by-model [in-file]
(with-open [in-file (io/reader in-file)]
(loop [results {}
rows (doall (csv/read-csv in-file))]
(if (empty? rows)
results
(let [[year reg engine mileage price kind] (first rows)
model (str year "/" engine "/" kind)
cars (get-in results [model] [])]
(recur (assoc-in results [model] (conj cars {:mileage mileage
:price price}))
(rest rows)))))))
(println (group-car-data-by-model (project-path "mx5.csv")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment