This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) | |
{} |