Given the following collection as input

(def input ({:a "a", :b "b"} {:b "b", :c "c"} {:d "d", :e "e"}))

I want to be able to return a subset of the contained maps for each map that 
contains at least one key from a list of provided keys.

for example:

(filter-keys input [:a :b])
;; out -> ({:a "a", :b "b"} {:b "b", :c "c"}))

(filter-keys input [:e f])
;; out -> ({:d "d", :e "e"})


Here is how I implemented this:


VERSION 1

(defn filter-keys [coll kset]
  (letfn [(keys-found [m]
           (-> m keys set (clojure.set/intersection kset))) 
          (any-keys-found? [m]
            (< 0 (count (keys-found m))))]
    (filter any-keys-found? coll)))


VERSION 2

(defn filter-keys [coll keyseq]
  (letfn [(any-keys-found? [m]
            (< 0 (count (select-keys m keyseq))))]
    (filter any-keys-found? coll)))


VERSION 3 ??

Is there something in clojure core or contrib that would make this easier?


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to