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/guides/destructuring.adoc
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -346,7 +346,7 @@ If you want it to be an error to supply a map that is missing certain keys (rath
346
346
;= throws Missing required key: "dob"
347
347
----
348
348
349
-
If you have keys you want to require but don't need to bind, you can list them after `&`, which will check for the presence of the listed keys but not create bindings.
349
+
If you have keys you want to require but don't need to bind, you can list them after `&`, [since 1.13] which will check for the presence of the listed keys but not create bindings.
350
350
351
351
[source,clojure]
352
352
----
@@ -357,9 +357,9 @@ If you have keys you want to require but don't need to bind, you can list them a
357
357
(let [{:syms! [fred ethel lucy & 'ricky]} m] ...
358
358
----
359
359
360
-
You can also document additional optional keys that you are not binding in the (:keys, :strs, and :syms) directives using `&`. [since 1.13]
360
+
You can also document additional optional keys that you are not binding in the (`:keys`, `:strs`, and `:syms`) directives using `&`.
361
361
362
-
To pass along or inspect the effective values (input or default) bound during destructuring, bind them to a map with `:select`. [since 1.13] It includes every key used anywhere in the form, including nested forms, but not keys used only in `:or`.
362
+
When you receive an input map, you often don't know exactly what it contains. To manage that uncertainty, Clojure developers often lock down the set of legal keys that their input maps accept, which creates brittle connections across system boundaries. When you need to know the keys you're dealing with or passing data to something that's intolerant of additional keys, while remaining flexible about what a process accepts, you can use `:select` [since 1.13]. The `:select` directive names a map that is a subset of the input map augmented by the defaults, limited to the the keys mentioned in the destructuring form.
Copy file name to clipboardExpand all lines: content/reference/special_forms.adoc
+4-11Lines changed: 4 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -286,7 +286,7 @@ In all of the sequential cases the __binding-form__s in the destructure binding
286
286
[[associative-destructuring]]
287
287
=== Associative destructuring
288
288
289
-
Map __binding-form__s create bindings by looking up values in collections like maps, sets, vectors, strings, and arrays (the latter three have integer keys). It consists of a map of __binding-form->key__ pairs, each __binding-form__ bound to the value in the _init-expr_ at the key provided. In addition, and optionally, an `:as` key in the binding form followed by a symbol binds that symbol to the entire __init-expr__. Also optionally, an `:or` key in the binding form followed by another map may be used to supply default values for some or all of the bindings if their keys are not found in the __init-expr__:
289
+
Map __binding-form__s create bindings by looking up values in collections like maps, sets, vectors, strings, and arrays (the latter three have integer keys). It consists of a map of __binding-form->key__ pairs, each __binding-form__ bound to the value in the _init-expr_ at the key provided. In addition, and optionally, an `:as` key in the binding form followed by a symbol binds that symbol to the entire __init-expr__. To supply default values for keys that are not present in the input map, you can use the `:or` directive, whose value is a map of binding names or [since 1.13] keys to default values.
290
290
291
291
[source,clojure]
292
292
----
@@ -296,13 +296,6 @@ 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
-
To supply a default for a key that has no local binding name, use the input key itself in the `:or` map instead. [since 1.13]
300
-
301
-
[source,clojure]
302
-
----
303
-
(let [{a :a, b :b, :or {:c 42}} ...
304
-
----
305
-
306
299
To capture the default values stated in the `:or` map for some purpose later in your code without restating them, you can use the `:defaults` directive [since 1.13], which binds a name to a map, created during destructuring, of keys to their default values.
307
300
308
301
[source,clojure]
@@ -368,16 +361,16 @@ Clojure 1.9 adds support for directly destructuring many keys (or symbols) that
368
361
-> [1 2]
369
362
----
370
363
371
-
You can ensure that required keys are present during map destructuring by using the checked variants of the `:keys`/`:syms`/`:strs` binding directives named `:keys!`/`:syms!`/`:strs!`, which throw an exception if any of the specified keys are missing from the destructured map. [since 1.13] These checked directives also accept `&`, after which you may list additional keys that are still required and checked, but not bound to a local name.
364
+
You can ensure that required keys are present during map destructuring by using the checked variants of the `:keys`/`:syms`/`:strs` binding directives named `:keys!`/`:syms!`/`:strs!`, [since 1.13] which throw an exception if any of the specified keys are missing from the destructured map. These checked directives also accept `&`, [since 1.13] after which you may list additional keys that are still required and checked, but not bound to a local name.
372
365
373
366
[source,clojure]
374
367
----
375
368
(let [{:keys! [fred ethel lucy & :ricky]} m] ... ;; m must have keys :fred, :ethel, :lucy, and :ricky
376
369
----
377
370
378
-
In the unchecked directives `:keys`, `:strs`, and `:syms`, `&` serves only as documentation: the listed keys are neither bound nor checked for presence. [since 1.13] The literal keys after `&` may be heterogeneous, as in `:keys [a & :b "c" 'd]`, but note that symbols must be quoted to distinguish them from binding names.
371
+
To document keys that you don't bind or to include them in `:select`, you can list them after `&` [since 1.13] in `:keys`, `:strs`, `:syms`. The literal keys after `&` may be heterogeneous, as in `:keys [a & :b "c" 'd]`, but note that symbols must be quoted to distinguish them from binding names.
379
372
380
-
To bind a name to a deep subset of the input map, augmented by defaults, use the `:select` directive. [since 1.13] It includes every key used anywhere in the destructuring form, including nested forms, but not keys used only in `:or`.
373
+
When you need to know the keys you're dealing with or passing data to something that's intolerant of additional keys, while remaining flexible about what a process accepts, you can use `:select` [since 1.13]. The `:select` directive names a map that is a subset of the input map augmented by the defaults, limited to the the keys mentioned in the destructuring form.
0 commit comments