Skip to content

Instantly share code, notes, and snippets.

@geoffeg
Created August 13, 2013 16:23
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 geoffeg/6222902 to your computer and use it in GitHub Desktop.
Save geoffeg/6222902 to your computer and use it in GitHub Desktop.
; Thanks to http://stackoverflow.com/questions/18202661/casting-values-of-a-map-to-their-proper-types-in-clojure
; for the basis for this, I needed to also support cases where a value in `input` was empty when the cast occured.
(def input {:a "1" :b "2.5" :c "more" :d "string" :e "keys" :f ""})
(def typetrans {:a #(Long/parseLong %) :b #(Double/parseDouble %) :f #(Integer/parseInt %)})
(defn cast-fields [csvmap]
(reduce-kv
(fn [acc k v]
(update-in acc [k] #(if-not (clojure.string/blank? %) (v %))))
input typetrans))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment