Skip to content

Instantly share code, notes, and snippets.

@mmower
Created September 4, 2013 18:26
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/d170188fcf88d8208ee8 to your computer and use it in GitHub Desktop.
Save mmower/d170188fcf88d8208ee8 to your computer and use it in GitHub Desktop.
(defn car-model
[year engine kind]
(str year "/" (format "%.1f" (float (read-string engine))) "/" (clojure.string/capitalize kind)))
(defn group-car-data-by-model
[car-data]
(reduce (fn [results row]
(let [[year reg engine mileage price kind] row
model (car-model year engine kind)
cars (get-in results [model] [])]
(assoc-in results [model] (conj cars {:mileage mileage
:price price})))) {} car-data))
(defn get-grouped-car-data
[in-path]
(with-open [in-file (io/reader in-path)]
(group-car-data-by-model (csv/read-csv in-file))))
(keys (get-grouped-car-data (project-path "mx5.csv")))
; ("2011/1.8/Convertible" "2012/1.8/Convertible" "2008/2.0/Convertible" "2008/1.8/Coupe" "2009/1.8/Coupe" "2009/2.0/Convertible" "2006/1.8/Convertible" "2008/1.8/Convertible" "2009/1.8/Convertible" "2010/2.0/Coupe" "2011/2.0/Coupe" "2010/1.8/Coupe" "2010/2.0/Convertible" "2011/2.0/Convertible" "2011/1.8/Coupe" "2012/1.8/Coupe" "2008/2.0/Coupe" "2009/2.0/Coupe" "2010/1.8/Convertible")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment