Skip to content

Instantly share code, notes, and snippets.

@alexpw
Forked from geoffeg/map-cast2.clj
Last active December 21, 2015 00:48
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 alexpw/6223055 to your computer and use it in GitHub Desktop.
Save alexpw/6223055 to your computer and use it in GitHub Desktop.
(defmulti type-cast (fn [k v] k))
(defmethod type-cast :int [k v] (Integer/parseInt v))
(defmethod type-cast :float [k v] (Float/parseFloat v))
(defmethod type-cast :default [k v] v)
(def input {:int "1", :float ".2", :str "three" :blank ""})
(reduce (fn [xs [k v]]
(assoc xs k (type-cast k v)))
{}
input)
=> {:int 1 :blank "" :str "three" :float 0.2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment