You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/reference/special_forms.adoc
+39-4Lines changed: 39 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -296,6 +296,26 @@ Map __binding-form__s create bindings by looking up values in collections like m
296
296
->[5 3 6 {:c 6, :a 5}]
297
297
----
298
298
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
+
299
319
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:
300
320
301
321
[source,clojure]
@@ -332,14 +352,14 @@ In the case of using prefixed keys, the bound symbol name is the same as the rig
332
352
-> 42
333
353
----
334
354
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:
336
356
337
357
[source,clojure]
338
358
----
339
-
(let [{:keys [fred ethel lucy & ricky]} m] ... ;; ricky is not bound
359
+
(let [{:keys [fred ethel lucy & :ricky]} m] ...
340
360
----
341
361
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.
343
363
344
364
Clojure 1.9 adds support for directly destructuring many keys (or symbols) that share the same namespace using the following destructuring key forms:
345
365
@@ -359,7 +379,22 @@ Clojure 1.9 adds support for directly destructuring many keys (or symbols) that
359
379
-> [1 2]
360
380
----
361
381
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}]
0 commit comments