Skip to content

Instantly share code, notes, and snippets.

@alexpw
Forked from rbutler/badmaps.clj
Created September 12, 2013 15:09
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/6539083 to your computer and use it in GitHub Desktop.
Save alexpw/6539083 to your computer and use it in GitHub Desktop.
(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