Skip to content

Commit 48f4630

Browse files
committed
Adding docs for new :or, :select, and :defaults
1 parent 5b9ebd1 commit 48f4630

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

content/reference/special_forms.adoc

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,26 @@ Map __binding-form__s create bindings by looking up values in collections like m
296296
->[5 3 6 {:c 6, :a 5}]
297297
----
298298

299+
Since Clojure 1.13, the `:or` directive also allows the use of explicit keys:
300+
301+
[source,clojure]
302+
----
303+
(let [{a :a, b :b, c :c, :as m :or {a 2 b 3 :c 42}} {:a 5}]
304+
[a b c m])
305+
306+
->[5 3 42 {:c 6, :a 5}]
307+
----
308+
309+
Also since Clojure 1.13, Clojure allows you to bind a name to a map of keys mapped to their default values, based on what you've said in `:or` per above:
310+
311+
[source,clojure]
312+
----
313+
(let [{a :a, b :b, c :c, :as m :or {a 2 b 3 :c 42} :defaults df} {:a 5}]
314+
[a b c df])
315+
316+
->[5 3 42 {:a 2, :b 3, :c 42}]
317+
----
318+
299319
It is often the case that you will want to bind symbols with the same name as the corresponding map keys. The `:keys` directive addresses the redundancy often found in the binding __binding-form->key__ pairs:
300320

301321
[source,clojure]
@@ -332,14 +352,14 @@ In the case of using prefixed keys, the bound symbol name is the same as the rig
332352
-> 42
333353
----
334354

335-
As of Clojure 1.13, map destructuring directives allow you to specify keys after `&` which will not be bound, but serve as documentation:
355+
As of Clojure 1.13, associative destructuring directives allow you to specify literal keys after `&` that serve as documentation:
336356

337357
[source,clojure]
338358
----
339-
(let [{:keys [fred ethel lucy & ricky]} m] ... ;; ricky is not bound
359+
(let [{:keys [fred ethel lucy & :ricky]} m] ...
340360
----
341361

342-
There are similar `:strs` and `:syms` directives for matching string and symbol keys, the latter also allowing prefixed symbol keys since Clojure 1.6, and support for `&` since Clojure 1.13.
362+
There are similar `:strs` and `:syms` directives for matching string and symbol keys, the latter also allowing prefixed symbol keys since Clojure 1.6. The literal keys after `&` for these directives match the form of the keys handled, e.g. `:strs [a & "b"]` and `:syms [a & 'b]`. For `:syms` the use of quote is required to distinguish between a binding name and a literal symbol.
343363

344364
Clojure 1.9 adds support for directly destructuring many keys (or symbols) that share the same namespace using the following destructuring key forms:
345365

@@ -359,7 +379,22 @@ Clojure 1.9 adds support for directly destructuring many keys (or symbols) that
359379
-> [1 2]
360380
----
361381

362-
As of Clojure 1.13, you can now ensure that required keys are bound during map destructuring by using the new checked variants of the `:keys`/`:syms`/`:strs` directives - `:keys!`/`:syms!`/`:strs!`, which will throw if the key is not present. Once again, the use of `&` in these checked variants will not bind names, but those keys are still checked in the destructured map.
382+
As of Clojure 1.13, you can now ensure that required keys are bound during map destructuring by using the new checked variants of the `:keys`/`:syms`/`:strs` directives - `:keys!`/`:syms!`/`:strs!`, which will throw if the key is not present. These checked directives also support the use of `&` and will check the presence of the specified keys in the destructured map.
383+
384+
[source,clojure]
385+
----
386+
(let [{:keys! [fred ethel lucy & :ricky]} m] ... ;; m must have keys :fred, :ethel, :lucy, and :ricky
387+
----
388+
389+
As of Clojure 1.13, supports a directive `:select` that binds a name to a map, created during destructuring, containing only those keys from the input map, or the defaults if not present in the input, that are used anywhere in the destructuring form (other than in `:or`), including in nested destructuring. Thus it is a (deep) subset of the input augmented by defaults.
390+
391+
[source,clojure]
392+
----
393+
(let [{:keys [a b & :c] :as m :or {a 42 b 3 :c 4} :select sm :defaults df} {:a 5}]
394+
[a b m sm df])
395+
396+
-> [5 3 {:a 1} {:c 4, :b 3, :a 5} {:a 42, :b 3, :c 4}]
397+
----
363398

364399
[[keyword-arguments]]
365400
=== Keyword Arguments

0 commit comments

Comments
 (0)