Maybe this?
user> (defn ensure-mandatory
" coll - keys of required maps
maps - collection of maps"
[coll maps]
(loop [adding (clojure.set/difference
(set coll)
(->> maps (map keys) flatten set))
all maps]
(if-let [k (first adding)]
(recur
(rest adding)
(conj all (hash-map k nil)))
all)))
'user/ensure-mandatory#
user> (def my-map
[{:3 true}
{:4 true}
{"c" :c-value}
{"d" :d-value}])
'user/my-map#
user> (def mandatory [:1 "2" "c"])
'user/mandatory#
user> (ensure-mandatory mandatory my-map)
[{:3 true} {:4 true} {"c" :c-value} {"d" :d-value} {:1 nil} {"2" nil}]
20.11.2013, 11:33, "John Mastro" <[email protected]>:
> 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.
--
--
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.