0

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?

4

1 回答 1

2

这可以zipmap通过两次使用相同的集合来实现:

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

但我认为使用它实际上更清楚into

(fn [item]
  (into {}
        (map (fn [tag]
               [tag ($x:text (str "./" (name tag)) item)])
             [:tag1 :tag2])))
于 2015-01-28T17:05:05.427 回答