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

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

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 via email to