Skip to content

Instantly share code, notes, and snippets.

View alexpw's full-sized avatar

Alex Walker alexpw

View GitHub Profile
@alexpw
alexpw / badmaps.clj
Created September 12, 2013 15:09 — forked from rbutler/badmaps.clj
(let [qparams {"cat" "meow"}
parammap {"cat" #(println "feline says" %), "dog" #(println "canine says" %)}
foundparam (into {} (filter #(contains? parammap (key %)) qparams))]
((get parammap (key (first foundparam))) (val (first foundparam))))
;> feline says meow
; Cleaner?
(let [queryparams {:cat "meow", :cheese "nonsense"}
functionmap {:cat #(println "feline says" %), :dog #(println "canine says" %)}
selectedkey (first (select-keys functionmap (keys queryparams)))]
@alexpw
alexpw / map-cast2.clj
Last active December 21, 2015 00:48 — forked from geoffeg/map-cast2.clj
(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)))
{}