This was my first thought (quite close to Jim's):
(def the-maps [{:key 3 :value 30} {:key 4 :value 40}])
(def mandatory-keys [1 2 3 4 5])
(defn find-missing-keys [maps keys]
(let [found (into #{} (map :key maps))]
(remove #(contains? found %) keys)))
(defn ensure-mandatory-keys [maps]
(let [missing (find-missing-keys maps mandatory-keys)]
(sort-by :key (reduce (fn [result key]
(conj result {:key key :value nil}))
maps
missing))))
(ensure-mandatory-keys the-maps)
;; ({:key 1, :value nil}
;; {:key 2, :value nil}
;; {:key 3, :value 30}
;; {:key 4, :value 40}
;; {:key 5, :value nil})
- John
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.