Zuriar
Zuriar

Reputation: 11734

Iterate over Clojure vector and build up a vector of sets

I am building up a datomic transaction. I have

(def codes 
   [:first-code :second-code :third-code])

I need to produce a vector of sets in this format:

[
  {:db/id (d/tempid :db.part/region)
   :db/ident :first-code}
  {:db/id (d/tempid :db.part/region)
   :db/ident :second-code}
  {:db/id (d/tempid :db.part/region)
   :db/ident :third-code}
]

I can't figure out how to do this idiomatically.

Upvotes: 0

Views: 328

Answers (3)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13175

I think you meant "a vector of maps". The following code should be good:

(mapv
  (fn [code]
    {:db/id '(d/tempid :db.part/region)
     :db/ident code})
  codes)

Upvotes: 3

superkonduktr
superkonduktr

Reputation: 655

You may want to use for comprehension:

(let [id '(d/tempid :db.part/region)]
  (vec
    (for [code codes]
      {:db/id id
       :db/ident code})))

Or map (mapv if you specifically want a vector):

(let [id '(d/tempid :db.part/region)]
  (->> codes
       (mapv (fn [code]
               {:db/id id
                :db/ident code}))))

Upvotes: 2

Istvan
Istvan

Reputation: 8552

I am pretty sure there is an easier way, but this would be a good start:

(vec 
  (map #(into {} {:db/id '(d/tempid :db.part/region) :db/ident %}) codes))

Upvotes: 1

Related Questions