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
+46-4Lines changed: 46 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -273,7 +273,25 @@ Associative destructuring, however, also allows you to supply a default value if
273
273
;= Category not found
274
274
----
275
275
276
-
The value for `:or` is a map where the bound symbol (here `category`) is bound to the expression `"Category not found"`. When category is not found in `client`, it is instead found in the `:or` map and bound to that value instead.
276
+
The value for `:or` is a map where the bound symbol (here `category`) is bound to the expression `"Category not found"`. When category is not found in `client`, it is instead found in the `:or` map and bound to that value instead. When used this way, the default is for the key associated with that binding name. If you haven't yet used that binding name in a binding elsewhere, it is not associated with any key and the entry is a no-op.
277
+
278
+
You may also specify values either via explicit keys in the `:or` map, which was added in Clojure 1.13.
279
+
280
+
[source,clojure]
281
+
----
282
+
(let [{category :category, :or {:category "Category again not found"}} client]
283
+
(println category))
284
+
;= "Category again not found"
285
+
----
286
+
287
+
If you wish to capture the default values stated in the `:or` map, the `:defaults` directive, added in Clojure 1.13, binds a name to a map, created during destructuring, of keys to their default values. You would use `:defaults` when you want those same default values for some purpose later in your code, and don't want to restate them.
;= {:category "Category not found", :comms "email"}
294
+
----
277
295
278
296
In sequential destructuring, you generally bind unneeded values with an `_`. Since associative destructuring doesn't require traversing the entire structure, you can simply omit any keys you don't plan on using from the destructuring form.
279
297
@@ -286,16 +304,18 @@ If you need access to the entire map, you can use the `:as` key to bind the enti
286
304
;= The name from {:name Super Co., :location Philadelphia, :description The world wide leader in plastic table-ware.} is Super Co.
287
305
----
288
306
289
-
The `:as`and `:or` keywords can be combined in a single destructuring.
307
+
The `:as`, `:or`, and `:defaults` keywords can be combined in a single destructuring.
(let [{a :a, x :x, :or {x "Not found!"}, :as all, :defaults df} my-map]
295
313
(println "I got" a "from" all)
296
-
(println "Where is x?" x))
314
+
(println "Where is x?" x)
315
+
(println "Defaults are" df))
297
316
;= I got A from {:a "A" :b "B" :c 3 :d 4}
298
317
;= Where is x? Not found!
318
+
;= Defaults are {:x Not found!}
299
319
----
300
320
301
321
You might have noticed that our original example still contains redundant information (the local binding name and the key name) in the associative destructuring form. The `:keys` key can be used to further remove the duplication:
@@ -326,6 +346,28 @@ The `:keys` key is for associative values with keyword keys, but there are also
326
346
;= Jane Doe
327
347
----
328
348
349
+
Additionally, since Clojure 1.13 there are checked versions of the binding directives named `:keys!`, `:strs!`, and `:syms!` that throw an exception if the keys specified are not present in the destructured map.
As of Clojure 1.13, all of the binding directives allow you to specify literal keys after `&` for documentation:
359
+
360
+
[source,clojure]
361
+
----
362
+
(let [{:keys [fred ethel lucy & :ricky]} m] ...
363
+
364
+
(let [{:strs [fred ethel lucy & "ricky"]} m] ...
365
+
366
+
(let [{:syms [fred ethel lucy & 'ricky]} m] ...
367
+
----
368
+
369
+
The use of `&` in the unchecked versions allow you to specify accepted keys, but its use in the checked variants is more strict as the keys specified are still required and will throw if not present.
370
+
329
371
Associative destructuring can be nested and combined with sequential destructuring as needed.
Copy file name to clipboardExpand all lines: content/reference/special_forms.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -379,7 +379,7 @@ Clojure 1.9 adds support for directly destructuring many keys (or symbols) that
379
379
-> [1 2]
380
380
----
381
381
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.
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` binding directives - `:keys!`/`:syms!`/`:strs!`, which will throw an exception if the keys specified are not present in the destructured map. These checked directives also support the use of `&` and will check the presence of the specified keys in the destructured map.
0 commit comments