-
-
Save alexpw/6539083 to your computer and use it in GitHub Desktop.
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)))] | |
((val selectedkey) (queryparams (key selectedkey)))) | |
; Cleanest? -alex | |
(let [q-params {:cat "meow", :cheese "nonsense"} | |
f-map {:cat #(str "feline says " %) | |
:dog #(str "canine says " %)} | |
[k f] (first (select-keys f-map (keys q-params)))] | |
(prn (f (q-params k)))) | |
;; WHAT DOES THE FOX SAY? | |
(let [qparams {:cat "meow" :cheese "nonsense" :dog "woof" :fox "nininininininini"} | |
fmap {:cat #(str "feline says " %) :dog #(str "canine says " %) :rat #(str "nasty rats are gross " %)} | |
fn-map (remove #(nil? (:fn (val %))) | |
(reduce (fn [m [k v]] | |
(assoc-in m | |
[k] | |
{:fn (fmap k) :param v})) | |
{} | |
qparams))] | |
(for [[k v] fn-map] ((v :fn) (v :param)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment