Odinodin
Odinodin

Reputation: 2177

How to get a value from a list of maps in 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?

Upvotes: 3

Views: 5120

Answers (2)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91534

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.

Upvotes: 5

huon
huon

Reputation: 102016

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.

Upvotes: 4

Related Questions