What about merging with a default map?

(defn my-function [params] 
  (let [params (merge {:my-key 5} params)]
    (str "my-key value is " (:my-key params))))


Well, I need to do something more "clever" because I am passing the whole 
> map to another function which does validation on the key-value pairs.
> So....i guess I need to append those default associations to the original 
> map myself...
>
> Something like...
>
> *(defn my-function [{:keys [my-key] :or {my-key 5} :as params}] 
>> **  (let [params (if (nil? (:my-key params)) (assoc params :my-key 
>> my-key) params)
>> **    (str "my-key value is " (:my-key params)))*
>
> *
> *
> *...but it's too damn ugly, need to think about it a bit more.*
>
> On Friday, May 3, 2013 10:22:59 PM UTC+3, Sean Corfield wrote:
>>
>> The :as key binds the entire _original_ map. 
>>
>> The :or defaults apply only to the bound variables extracted from the 
>> map. 
>>
>> It caught me out when I first used map destructuring but I soon got used 
>> to it. 
>>
>>
>> On Fri, May 3, 2013 at 12:08 PM, Ryan <areka...@gmail.com> wrote: 
>> > Thanks Anthony for your reply but I was already aware of that. I was 
>> hoping 
>> > for some solution which would took care of the binding if :as was 
>> provided 
>> > and would append the key-value to the :as parameter. 
>> > 
>> > I probably need to do that myself....but you never know, someone else 
>> might 
>> > have done it and has an elegant proposal :) 
>> > 
>> > Ryan 
>> > 
>> > 
>> > On Friday, May 3, 2013 10:04:41 PM UTC+3, Anthony Rosequist wrote: 
>> >> 
>> >> The value of :my-key (or the default, if :my-key doesn't exist) is 
>> being 
>> >> bound to the symbol my-key, so all you need to do is this: 
>> >> 
>> >>> (defn my-function [{:keys [my-key] :or {my-key 5} :as params}] 
>> >>>   (str "my-key value is " my-key)) 
>> >> 
>> >> 
>> >> On Friday, May 3, 2013 1:52:46 PM UTC-5, Ryan wrote: 
>> >>> 
>> >>> Hello all, 
>> >>> 
>> >>> I have a question regarding the default values when destructuring 
>> with 
>> >>> :keys 
>> >>> 
>> >>> Let's assume that we have the following function: 
>> >>> 
>> >>>> (defn my-function [{:keys [my-key] :or {my-key 5} :as params}] 
>> >>>>   (str "my-key value is " (:my-key params))) 
>> >>> 
>> >>> 
>> >>> A few test runs: 
>> >>> 
>> >>>> 
>> >>>> user=> (my-function {:my-key 1}) 
>> >>>> "my-key value is 1" 
>> >>> 
>> >>> 
>> >>> Works as expected. 
>> >>> 
>> >>>> user=> (my-function {:another-key "hello"}) 
>> >>>> "my-key value is " 
>> >>> 
>> >>> 
>> >>> As you can see here, my-key value is nil even thought I have a 
>> default 
>> >>> value of 5. 
>> >>> From the above observation, it seems that :as will not fill in the 
>> >>> missing keys with the default values if those are provided as in my 
>> case. 
>> >>> 
>> >>> Does anyone have a solution for this? 
>> >>> 
>> >>> Thank you for your time 
>> >>> 
>> >>> Ryan 
>> > 
>> > -- 
>> > -- 
>> > You received this message because you are subscribed to the Google 
>> > Groups "Clojure" group. 
>> > To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com. 
>> > For more options, visit https://groups.google.com/groups/opt_out. 
>> > 
>> > 
>>
>>
>>
>> -- 
>> Sean A Corfield -- (904) 302-SEAN 
>> An Architect's View -- http://corfield.org/ 
>> World Singles, LLC. -- http://worldsingles.com/ 
>>
>> "Perfection is the enemy of the good." 
>> -- Gustave Flaubert, French realist novelist (1821-1880) 
>>
>

-- 
-- 
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