Search code examples
clojurezipmap

Create a map out of vector of tags and a form


I want to write a generalized version out of this code

(fn[item] 
 {:tag1 ($x:text "./tag1" item) 
  :tag2 ($x:text "./tag2" item)})

I was thinking of using zipmap, because I'd use a vector of tags, like this

[:tag1 :tag2],

but don't know how to make a generalized version of form

($x:text "./tag1" item)

for any given item from the vector.

Any ideas?


Solution

  • This is possible with zipmap, by using the same collection twice:

    (fn [item]
      (let [tags [:tag1 :tag2]]
        (zipmap tags
                (map (fn [tag]
                       ($x:text (str "./" (name tag)) item))
                     tags))))
    

    but I think it is actually clearer using into:

    (fn [item]
      (into {}
            (map (fn [tag]
                   [tag ($x:text (str "./" (name tag)) item)])
                 [:tag1 :tag2])))