Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created January 14, 2015 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kindlychung/3ff3b591a74af81d4124 to your computer and use it in GitHub Desktop.
Save kindlychung/3ff3b591a74af81d4124 to your computer and use it in GitHub Desktop.
(defn mapify
"Return a seq of maps like {:name \"Edward Cullen\" :glitter-index 10}"
[rows]
(let [;; headers becomes the seq (:name :glitter-index)
headers (map #(get headers->keywords %) (first rows))
;; unmapped-rows becomes the seq
;; (["Edward Cullen" "10"] ["Bella Swan" "0"] ...)
unmapped-rows (rest rows)]
;; Now let's return a seq of {:name "X" :glitter-index 10}
(map (fn [unmapped-row]
;; We're going to use map to associate each header with its
;; column. Since map returns a seq, we use "into" to convert
;; it into a map.
(into {}
;; notice we're passing multiple collections to map
(map (fn [header column]
;; associate the header with the converted column
[header ((get conversions header) column)])
headers
unmapped-row)))
unmapped-rows)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment