Named arguments in HEAD and prettier default arguments for :keys destructuring

56 views
Skip to first unread message

Per Vognsen

unread,
Mar 23, 2010, 10:56:25 PM3/23/10
to clo...@googlegroups.com
Those of you following the clojure repo on GitHub may have noticed this commit:

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

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

Douglas Philips

unread,
Mar 23, 2010, 11:00:19 PM3/23/10
to clo...@googlegroups.com
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

Stefan Kamphausen

unread,
Mar 25, 2010, 8:27:42 AM3/25/10
to Clojure
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

Laurent PETIT

unread,
Mar 25, 2010, 8:41:41 AM3/25/10
to clo...@googlegroups.com
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.v...@gmail.com>:

> --
> 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
>
> To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
>

Reply all
Reply to author
Forward
0 new messages