Skip to content

Commit 243dc2e

Browse files
committed
To get a map like the input map with defaults substituted for missing keys, use the :all directive. :all traverses deeply through nested maps.
1 parent 5b44d35 commit 243dc2e

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

content/guides/destructuring.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,17 @@ When you receive an input map, you often don't know exactly what it contains. To
370370
;= {:c "unknown", :b 3, :a 5}
371371
----
372372

373+
To get a map like the input map with defaults substituted for missing keys, use the `:all` directive. [since 1.13] `:all` traverses deeply through nested maps.
374+
375+
[source,clojure]
376+
----
377+
(let [{:keys [a & :b] :as m :or {:a 42 :b 3} :all all-m} {}]
378+
[a all-m])
379+
380+
381+
-> [42 {:a 42, :b 3}]
382+
----
383+
373384
Associative destructuring can be nested and combined with sequential destructuring as needed.
374385

375386
[source,clojure]

content/reference/special_forms.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,32 @@ When you need to know the keys you're dealing with or passing data to something
380380
-> [5 3 {:a 1} {:c 4, :b 3, :a 5} {:a 42, :b 3, :c 4}]
381381
----
382382

383+
To get a map like the input map with defaults substituted for missing keys, use the `:all` directive. [since 1.13] `:all` traverses deeply through nested maps.
384+
385+
[source,clojure]
386+
----
387+
(let [{:keys [a & :b] :as m :or {:a 42 :b 3} :all all-m} {}]
388+
[a all-m])
389+
390+
391+
-> [42 {:a 42, :b 3}]
392+
----
393+
394+
Defaults are applied at every nested level that contains an `:or` directive.
395+
396+
[source,clojure]
397+
----
398+
(let [nested-map {:a 1, :nested {:aa 10}}
399+
400+
{:keys [a & :b] :as m :or {:a 42 :b 3} :all all-m
401+
{:keys [aa bb] :or {:bb 30}} :nested}
402+
nested-map]
403+
all-m)
404+
405+
406+
-> {:a 1, :b 3, :nested {:bb 30, :aa 10}}
407+
----
408+
383409
[[keyword-arguments]]
384410
=== Keyword Arguments
385411

0 commit comments

Comments
 (0)