Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter values on maps and return results

I want to write a filter function for maps, That is going to filter the given value on all id name and surname props.

If possible I especially need one filter func code that is similar to my code. I would like to compare and understand my error. And then if possible can you share with me some other ideas and ways to do that? That would be nice for me and of course the community who going to need it in the future. 🙂

;this is my input map
(def my-map {1 {:id 1 :name "ali" :surname "veli"}
             2 {:id 2 :name "batu" :surname "can"}
             3 {:id 3 :name "bar" :surname "foo"}
             4 {:id 4 :name "zap" :surname "can"}
             }) 

;my expected result for "ba" ---> there are two "ba" value on name prop
{2 {:id 2 :name "batu" :surname "can"}
 3 {:id 3 :name "bar" :surname "foo"}}

;my expected result for "ca" ---> there are two "ca" value on surname prop
{2 {:id 2 :name "batu" :surname "can"}
 4 {:id 4 :name "zap" :surname "can"}}

;my expected result for "z" ---> there is just one "z" value on surname prop
{4 {:id 4 :name "zap" :surname "can"}}

;This is my search function ---> it is search just on surname at the moment(to find my ;error debugging :))

(defn filter-db-by-surname [?s db]
  (->> (for [lenght (range 1 (+ 1 (count my-map)))]
         (db lenght))                                           
       (filter
         (fn
           [{:keys [id name surname]} ]
           (let [i id
                 k name
                 v surname]
             (if (str/includes? (str/lower-case (str v)) (str/lower-case (str ?s)))
               (into [] {id (my-map i)})
               ())
             )))))


(filter-db-by-surname "ca" my-map



MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

(require '[clojure.string :as str])

(defn filter-map-by-str [data s & fields]
  (into (empty data)
        (filter (fn [[_k v]]
                  (some #(str/includes? (% v) s) fields)))
        data))

(def my-map {1 {:id 1 :name "ali" :surname "veli"}
             2 {:id 2 :name "batu" :surname "can"}
             3 {:id 3 :name "bar" :surname "foo"}
             4 {:id 4 :name "zap" :surname "can"}
             })

(filter-map-by-str my-map "z" :name :surname)
=> {4 {:id 4, :name "zap", :surname "can"}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading