Search code examples
data-structuresclojureclojurescript

How to filter vector of maps by multiple keys in Clojure


Assume we have a datastructure like this one:

(def data
     (atom [{:id 1 :first-name "John1" :last-name "Dow1" :age "14"}
            {:id 2 :first-name "John2" :last-name "Dow2" :age "54"}
            {:id 3 :first-name "John3" :last-name "Dow3" :age "34"}
            {:id 4 :first-name "John4" :last-name "Dow4" :age "12"}
            {:id 5 :first-name "John5" :last-name "Dow5" :age "24"}]))

I have learned how to filter it by one key, for example:

(defn my-filter
  [str-input]
  (filter #(re-find (->> (str str-input)
                         (lower-case)
                         (re-pattern))
                         (lower-case (:first-name %)))
            @data))

> (my-filter "John1")
> ({:last-name "Dow1", :age "14", :first-name "John1", :id 1})

But now I'm a little bit confused on how to filter data by :first-name, :last-name and :age simple way?

Update: Sorry for being not too clear enough in explanation of what the problem is... Actually, I want all keys :first-name, :last-name and :age to paticipate in filter function, so that, if str-input doesn't match :first-name's val, check if it matches :last-name's val and so on.

Update 2: After trying some-fn, every-pred and transducers, I didn't get what I need, e.g. regex in filter predicates, I guess it's a lack of knowledge for now. So, I ended up with this function which works fine, but the code is ugly and duplicated. How I can get rid of code duplication?

(defn my-filter [str-input]
  (let [firstname (filter #(re-find (->> (str str-input)
                                         (upper-case)
                                         (re-pattern))
                                    (upper-case (:first-name %)))
                     @data)
        lastname (filter #(re-find (->> (str str-input)
                                        (upper-case)
                                        (re-pattern))
                                   (upper-case (:last-name %)))
                    @data)
        age (filter #(re-find (->> (str str-input)
                                   (upper-case)
                                   (re-pattern))
                              (upper-case (:age %)))
               @data)]
    (if-not (empty? firstname)
      firstname
      (if-not (empty? lastname)
        lastname
        (if-not (empty? age)
          age)))))

Solution

  • This can also be achieved with the help of functional composition, e.g. you can use every-pred function, which creates a function, checking if all the preds are truthy for its arguments, and use it to filter data. For example if you want to find all items with odd :id value having :last-name of "Dow1", "Dow2", or "Dow3" and :age starting with \3:

    user> (def data
      [{:id 1 :first-name "John1" :last-name "Dow1" :age "14"}
       {:id 2 :first-name "John2" :last-name "Dow2" :age "54"}
       {:id 3 :first-name "John3" :last-name "Dow3" :age "34"}
       {:id 4 :first-name "John4" :last-name "Dow4" :age "12"}
       {:id 5 :first-name "John5" :last-name "Dow5" :age "24"}])
    
    user> (filter (every-pred (comp odd? :id)
                              (comp #{"Dow1" "Dow2" "Dow3"} :last-name)
                              (comp #{\3} first :age))
                  data)
    
    ;;=> ({:id 3, :first-name "John3", :last-name "Dow3", :age "34"})
    

    another way to do it, is to use transducers:

    user> (sequence (comp (filter (comp odd? :id))
                          (filter (comp #{"Dow1" "Dow2" "Dow3"} :last-name)))
                    data)
    

    notice that the actual filtering would happen just once for every item, so it won't create any intermediate collections.

    Update

    According to your update you need to keep the value when any of the predicates is true, so you can use some function instead of every-pred:

    user> (filter #(some (fn [pred] (pred %))
                         [(comp odd? :id)
                          (comp #{"Dow1" "Dow2" "Dow4"} :last-name)
                          (comp (partial = \3) first :age)])
                  data)
    ;;=> ({:id 1, :first-name "John1", :last-name "Dow1", :age "14"} {:id 2, :first-name "John2", :last-name "Dow2", :age "54"} {:id 3, :first-name "John3", :last-name "Dow3", :age "34"} {:id 4, :first-name "John4", :last-name "Dow4", :age "12"} {:id 5, :first-name "John5", :last-name "Dow5", :age "24"})