0
votes

I have a vector of maps

cards_vector = [{...} {...} ...]

and an atom

(def cards_map (atom {})

For each map in cards_vector, I want to add the map to cards_map with key card-n, where n increments from 1 to count(cards_vector). So, cards-map should return

{:card-1 {...}
 :card-2 {...}
 ...
 :card-n {...}}
2
Why bother with :card-X as keys - what else would be in card-maps? Also please add the code you have tried and how it failed so we can improve on it. Right now it's not clear, what you are struggling with (hints: map-indexed, reset!, keyword)cfrick
you can also use (zipmap (map #(keyword (str "card-" (inc %))) (range)) data)leetwinski

2 Answers

2
votes

I propose this snippet:

(->> [{:a 1} {:b 2}]
     (map-indexed (fn [idx value] [(keyword (str "card-" idx)) value]))
     (into {}))
;; => {:card-0 {:a 1}, :card-1 {:b 2}}

But I agree with the comment of cfrick. Choosing a key with the shape :card-X doesn't seem to be really practical. But you can do it :)

1
votes

Another solution, closer to imperative programming but maybe less efficient than map-indexed:

(into {} (for [k (range (count @cards_map))] [(keyword (str "card-" k)) (nth @cards_map k)]))