On 19/11/13 11:42, Jim - FooBar(); wrote:
On 19/11/13 11:29, Colin Yates wrote:
Imagine the sequence represents a distribution between 1 and 5. The initial sequence might only be [{:key 3 :value 30} {:key 4 :value 40}]. I want it to be [{:key 1 :value nil} {:key 2 :value nil} {:key 3 :value 30} {:key 4 :value 40} {:key 5 :value nil}].


(def map-seq  [{:key 3 :value 30} {:key 4 :value 40}])
(def mandatory-keys (range 1 6))

(sort-by :key
(reduce
 #(if (some #{%2} (map :key %))
     %
    (conj % {:key %2 :value nil})) map-seq mandatory-keys))

=> ({:key 1, :value nil} {:key 2, :value nil} {:key 3, :value 30} {:key 4, :value 40} {:key 5, :value nil})


Jim


of course if the keys are known in advance, you don't want to calculate them every single time:

(let [ks (map :key map-seq)]
(sort-by :key
(reduce
 #(if (some #{%2} ks) %
   (conj % {:key %2 :value nil})) map-seq mandatory-keys)))

Jim

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to