Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from a list of maps in Clojure

Tags:

clojure

I have a list of maps from which I want to retrieve the value of "field" given a certain "columnId".

(def field-to-column-mapping
   [{"field" "first_name", "columnId" "1"}
    {"field" "last_name", "columnId" "2"}
    {"field" "phone", "columnId" "3"}])


(defn get-field-given-column [id]
  ((some #(if (= id (get % "columnId")) %)
         field-to-column-mapping)
   "field"))

 ;; Gets "first_name"
 (get-field-given-column "1")

This works, but my gut tells me there has to be a cleaner way to do it.

Is there a better way to do it?

like image 980
Odinodin Avatar asked Jan 28 '26 23:01

Odinodin


2 Answers

Writing this using a for makes it perhaps a bit clearer while doing essentially the same thing as the example in your question:

(defn get-field-given-column [id] 
   (for [row field-to-column-mapping :when (= (row "columnId") id)] 
      (row "field")))

> (get-field-given-column "1")
("first_name")

This returns a sequence of answers, as it is possible for more than one row to have the same key. If there really will only be one, then add a call to first.

like image 92
Arthur Ulfeldt Avatar answered Jan 30 '26 18:01

Arthur Ulfeldt


If you are doing lots of look-ups without changing field-to-column-mapping, it might be worth constructing a "columnId" -> "field" map, e.g.

(def column-to-field
   (zipmap
     (map #(% "columnId") field-to-column-mapping)
     (map #(% "field") field-to-column-mapping)))
; {"3" "phone", "2" "last_name", "1" "first_name"}

Another way to define column-to-field would be (I'm not sure which way is better/more efficient):

(def column-to-field
   (apply hash-map
      (mapcat
         #(vector (% "columnId") (% "field"))
         field-to-column-mapping)))

Now (column-to-field "1") gives the desired result.

like image 40
huon Avatar answered Jan 30 '26 19:01

huon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!