Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Created April 13, 2013 15:19
Show Gist options
  • Save ikitommi/5378805 to your computer and use it in GitHub Desktop.
Save ikitommi/5378805 to your computer and use it in GitHub Desktop.
Filtering nested maps in Clojure
;; here only data used in filtering.
(def docs [{:schema {:info {:name "party"}}}
{:schema {:info {:name "bonus"}}}
{:schema {:info {:name "party"}}}])
;; anonymous function with get-in (72)
(filter (fn [doc] (= "party" (get-in doc [:schema :info :name]))) docs)
;; anonymous function & threading (66)
(filter (fn [doc] (-> doc :schema :info :name (= "party"))) docs)
;; anonymous function in shortcut form & get-in (60)
(filter #(= "party" (get-in % [:schema :info :name])) docs)
;; anonymous function in shortcut form & threading (54)
(filter #(-> % :schema :info :name (= "party")) docs)
;; macro for creating anonymous function for threading on input
(defmacro => [& body] `(fn [c#] (-> c# ~@body)))
;; clean & lean (51)
(filter (=> :schema :info :name (= "party")) docs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment