Skip to content

Instantly share code, notes, and snippets.

@stungeye
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save stungeye/adf3134a819004058dad to your computer and use it in GitHub Desktop.

Select an option

Save stungeye/adf3134a819004058dad to your computer and use it in GitHub Desktop.
EDS Lookup Table Refactor
(ns eds-lookup.core
(:gen-class))
(require '[clj-yaml.core :as yaml]
'[clojure-csv.core :as csv]
'[clojure.string :as str])
(def new-locations (yaml/parse-string (slurp "locations.yml")))
(def eds-locations (csv/parse-csv (slurp "eds-locations.csv")))
(defn new-location [location-code]
(new-locations (keyword (str/lower-case location-code))))
(defn update-location-vector [location-vector]
(let [[code _ facet limiter] location-vector]
[code (new-location code) facet limiter]))
(defn update-eds-locations [location-table]
(let [[header & table] eds-locations]
(cons header (map update-location-vector table))))
(defn -main [& args]
(spit "output" (csv/write-csv (update-eds-locations eds-locations))))
@stungeye

Copy link
Copy Markdown
Author

Changes:

  • Added -main to allow for lein run
  • Renamed locations to new-locations for clarity.
  • Renamed get-in-locations [location-key] to new-location [location-code] for clarity.
  • Reworked update-eds-locations to destructure the csv header from the table data.
  • Reworked update-location-vector to remove the if-let since the header is no longer present.
  • Reworked update-location-vector to destructure the location-vector and built new vector in place.

ghost commented Jul 20, 2015

Copy link
Copy Markdown

Oh, thanks - yeah, that's much better. I get what you've done. That makes a lot of sense.

I really felt the lack of a test in this case - I need to start getting into TDD with clojure. I also (for the first time) felt the lack of syntax tools, as in that Sublime video.

Cheers!

ghost commented Jul 20, 2015

Copy link
Copy Markdown

I just reread your refactor - it has the simplicity I knew I hadn't achieved with my attempt. In mine I could tell that it wasn't as clear as the Ruby version, which seemed strange, given what Clojure's like (it should be at least as clear), but with this everything is broken out and properly functional. This looks really good.

@stungeye

Copy link
Copy Markdown
Author

I did a poor man's TDD by running lein run && diff output output.golden where output.golden was your output.

I had to jump into the repl though on occasion, which I probably wouldn't have needed if I had written finer grain tests.

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