Another alternative way, added in Clojure 1.5, can be nice if you have a lot of conditionally-added key-value pairs:
(defn create-map [] > (cond-> {:base :map} > (pred1) (assoc :extra-key1 value) > (pred2) (assoc :extra-key2 value) > (pred3) (assoc :extra-key3 value) > (pred4) (assoc :extra-key4 value))) On Thu, Feb 21, 2013 at 7:41 AM, Alex Baranosky < alexander.barano...@gmail.com> wrote: > I'd often just write it like this: > > (defn create-map [] >> (merge {:foo "a" >> :bar "b" >> ... many more keys} >> (when (some-condition-true) >> {:extra-key value))) > > > Alex > > > On Thu, Feb 21, 2013 at 7:27 AM, Ryan <arekand...@gmail.com> wrote: > >> Thanks Meikel for your reply. >> >> It wasn't clear on my example, but I wish to use *merge *inside my >> create-map function. Do you think I should bind my hash-map to a let >> variable first and then apply merge, or do you have another proposal? What >> i mean is this: >> >> (defn create-key-value [value] >> >> (when (some-condition-true) {:extra-key value)) >> >> >> (defn create-map [] >>> (let [my-map {:foo "a" :bar "b"}] >>> (merge my-map (create-key-value "some-value"))) >> >> >> Is the above "clojurish" or is there a better way? For example if the >> my-map was a big hash-map, and not with just two pairs, wouldn't it be ugly >> with the above way? or..it's just fine and no better way is possible? >> >> Ryan >> >> On Thursday, February 21, 2013 5:02:21 PM UTC+2, Meikel Brandmeyer >> (kotarak) wrote: >>> >>> Hi, >>> >>> merge works with nil. So (merge (create-map) (create-key-value >>> "some-value")) should work as you intend with the functions as they are >>> defined now. >>> >>> Kind regards >>> Meikel >>> >>> -- >> -- >> 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. >> >> >> > > -- -- 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.