Anonymous user
Named parameters: Difference between revisions
→{{header|Clojure}}
(→{{header|XSLT}}: Added omit PureBasic) |
|||
Line 133:
Clojure doesn't have built-in support for named or keyword arguments, but you can use destructuring to achieve a similar effect.
<lang
(let [opts (merge {:bar 1 :baz 2} (apply hash-map opts))
{:keys [bar baz]} opts]
[bar baz]))</lang>
Clojure 1.2 supports destructuring of trailing arguments as a map:
<lang clojure>(defn foo [& {:keys [bar baz] :or {bar 1, baz 2}}]
[bar baz])</lang>
You can also use <code>defnk</code> from <code>clojure.contrib.def</code>, which is a macro that works similarly.
<lang
(defnk foo [:bar 1 :baz 2]
[bar baz])</lang>
Sample output for
<pre>user> (foo :baz 123)
|