In Clojure 1.2, you can destroy the rest argument just as if you had destroyed the map. This means that you can use 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"}
All you can do while destructuring a Clojure map can be done in the function's argument list, as shown above. Including use: or to define default values ββfor such arguments:
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. In addition, 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 it works the same overall.
And you can also have positional arguments that come before the keyword arguments:
user> (defn blah [xy & {:keys [key1 key2 key3] :or {key3 10}}] (str xy key1 key2 key3)) #'user/blah user> (blah "x" "Y" :key1 "Hai" :key2 " there") "xYHai there10"
They are not optional and must be provided.
In fact, you can destroy the rest argument, like 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 such things even in Clojure 1.1. However, file-style destructuring for keyword arguments appeared only in 1.2.
Rayne Jul 26 2018-10-18T00: 00-07
source share