tags:

views:

313

answers:

3

Does Clojure have named arguments? If so, can you please provide a small example of it?

+1  A: 

Do you perhaps mean named parameters? These aren't directly available, but you can use this vectors approach if you like, which may give you what you want.

At RosettaCode there's a deeper explanation on how to do this using destructuring.

Abel
Thanks for the answer! :-)
one-zero-zero-one
+19  A: 

In Clojure 1.2, you can destructure the rest argument just like you would destructure a map. This means you can do named non-positional keyword arguments. Here is an example:

user> (defn blah [& {:keys [key1 key2 key3]}] (str key1 key2 key3))
#'user/blah
user> (blah :key1 "Hai" :key2 " there" :key3 10)
"Hai there10"
user> (blah :key1 "Hai" :key2 " there")
"Hai there"
user> (defn blah [& {:keys [key1 key2 key3] :as everything}] everything)
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
{:key2 " there", :key1 "Hai"}

Anything you can do while destructuring a Clojure map can be done in a function's argument list as shown above. Including using :or to define defaults for the arguments like this:

user> (defn blah [& {:keys [key1 key2 key3] :or {key3 10}}] (str key1 key2 key3))
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
"Hai there10"

But this is in Clojure 1.2. Alternatively, in older versions, you can do this to simulate the same thing:

user> (defn blah [& rest] (let [{:keys [key1 key2 key3] :or {key3 10}} (apply hash-map rest)] (str key1 key2 key3)))
#'user/blah
user> (blah :key1 "Hai" :key2 " there")
"Hai there10"

and that works generally the same way.

And you can also have positional arguments that come before the keyword arguments:

user> (defn blah [x y & {:keys [key1 key2 key3] :or {key3 10}}] (str x y key1 key2 key3))
#'user/blah
user> (blah "x" "Y" :key1 "Hai" :key2 " there")
"xYHai there10"

These are not optional and have to be provided.

You can actually destructure the rest argument like you would any Clojure collection.

user> (defn blah [& [one two & more]] (str one two "and the rest: " more))
#'user/blah
user> (blah 1 2 "ressssssst")
"12and the rest: (\"ressssssst\")"

You can do this sort of thing even in Clojure 1.1. The map-style destructuring for keyword arguments only came in 1.2 though.

Rayne
Thanks for the answer. Lisp is GREAAAAT!!! :-)
one-zero-zero-one
You are very welcome. And yes it is. It certainly is. =)
Rayne
+9  A: 

In addition to Raynes' excellent answer, there is also a macro in clojure-contrib that makes life easier:

user=> (use '[clojure.contrib.def :only [defnk]])
nil
user=> (defnk foo [a b :c 8 :d 9] 
         [a b c d])
#'user/foo
user=> (foo 1 2)
[1 2 8 9]
user=> (foo 1 2 3)
java.lang.IllegalArgumentException: No value supplied for key: 3 (NO_SOURCE_FILE:0)
user=> (foo 1 2 :c 3)
[1 2 3 9]
Alex Taggart
I forgot to mention that! I was all caught up in showing the 10 thousand ways Clojure can destructure stuff. :p
Rayne