Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Named arguments in HEAD and prettier default arguments for :keys destructuring
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Per Vognsen  
View profile  
 More options Mar 23 2010, 10:56 pm
From: Per Vognsen <per.vogn...@gmail.com>
Date: Wed, 24 Mar 2010 09:56:25 +0700
Local: Tues, Mar 23 2010 10:56 pm
Subject: Named arguments in HEAD and prettier default arguments for :keys destructuring
Those of you following the clojure repo on GitHub may have noticed this commit:

http://github.com/richhickey/clojure/commit/29389970bcd41998359681d9a...

Now you can do things like this:

user=> (defn funkymonkey [x y z & {:keys [a b c]}] [x y z a b c])
#'user/funkymonkey
user=> (funkymonkey 1 2 3)
[1 2 3 nil nil nil]
user=> (funkymonkey 1 2 3 :b 5)
[1 2 3 nil 5 nil]
user=> (funkymonkey 1 2 3 :c 6 :a 4 :b 5)
[1 2 3 4 5 6]

Very nice! It feels smoothly integrated with the general destructuring
infrastructure. You can also supply default values with the :or
binder:

user=> (defn funkymonkey [x y z & {:keys [a b c] :or {a -1 b -2 c -3}]
[x y z a b c])
#'user/funkymonkey
user=> (funkymonkey 1 2 3)
[1 2 3 -1 -2 -3]
user=> (funkymonkey 1 2 3 :b 5)
[1 2 3 -1 5 -3]

The great thing about :keys is that it cuts down on redundancy: you
specify a symbol only once and it is dually interpreted as a map
keyword and a lexically bound symbol. Since :keys already expects a
flat sequence of symbols rather than arbitrary nested binding forms
(otherwise this trick of dual interpretation wouldn't work), you could
further cut down on the redundancy in the above :keys/:or idiom (which
I expect would become commonplace with named arguments) by letting
:keys elements optionally be two-element vectors with the second
element supplying the default value:

user=> (defn funkymonkey [x y z & {:keys [[a -1] [b -2] [c -3]]] [x y z a b c])

What do you think? I hacked this into my local version of core.clj's
destructure and it feels very natural to me.

-Per


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Douglas Philips  
View profile  
 More options Mar 23 2010, 11:00 pm
From: Douglas Philips <d...@mac.com>
Date: Tue, 23 Mar 2010 23:00:19 -0400
Local: Tues, Mar 23 2010 11:00 pm
Subject: Re: Named arguments in HEAD and prettier default arguments for :keys destructuring
On 2010 Mar 23, at 10:56 PM, Per Vognsen wrote:

> :keys elements optionally be two-element vectors with the second
> element supplying the default value:

> user=> (defn funkymonkey [x y z & {:keys [[a -1] [b -2] [c -3]]] [x  
> y z a b c])

> What do you think? I hacked this into my local version of core.clj's
> destructure and it feels very natural to me.

I like the existing arity overloading, and when I looked into the core  
source, was saddened to see the repeated code that handled optional  
parameters. This is one thing I like from Common Lisp, so I'm glad to  
see it here.
However, as a clojure newbie, I have no idea what subtle interactions  
this might have.

-Doug


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Kamphausen  
View profile  
 More options Mar 25 2010, 8:27 am
From: Stefan Kamphausen <ska2...@googlemail.com>
Date: Thu, 25 Mar 2010 05:27:42 -0700 (PDT)
Local: Thurs, Mar 25 2010 8:27 am
Subject: Re: Named arguments in HEAD and prettier default arguments for :keys destructuring
Hi,

I've no idea whether this is reasonable but when I read your post
suddenly the following thought appeared...

What if defn would accept either a vector for the parameters or a map?

(defn foo {:dont "know" :what "for"}
  ;; ...
 )

I did no deeper thinking on this at all, er, skip the "deeper", I
guess. :-)

Cheers,
Stefan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laurent PETIT  
View profile  
 More options Mar 25 2010, 8:41 am
From: Laurent PETIT <laurent.pe...@gmail.com>
Date: Thu, 25 Mar 2010 13:41:41 +0100
Local: Thurs, Mar 25 2010 8:41 am
Subject: Re: Named arguments in HEAD and prettier default arguments for :keys destructuring
Hi,

Interesting. Seems more readable, more DRY, indeed. Especially if this
becomes idiomatic for optional named arguments ...

And the general case could be:

(defn funkymonkey [x y z & {:keys [[a -1] [b -2] [c -3]], d :d, e [:e
-4], :or {b -3, g -6}} [x y z a b c])

?

Which leads to the question: which default value "wins" for b ? :)

2010/3/24 Per Vognsen <per.vogn...@gmail.com>:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »