Skip to content

Commit a6ab561

Browse files
committed
WiP, first contact example for :select
1 parent 0668641 commit a6ab561

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

content/guides/destructuring.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ The `:as`, `:or`, and `:defaults` keywords can be combined in a single destructu
315315
(println "Defaults are" df))
316316
;= I got A from {:a "A" :b "B" :c 3 :d 4}
317317
;= Where is x? Not found!
318-
;= Defaults are {:x Not found!}
318+
;= Defaults are {:x "Not found!"}
319319
----
320320

321321
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:
@@ -368,6 +368,17 @@ As of Clojure 1.13, all of the binding directives allow you to specify literal k
368368

369369
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.
370370

371+
Also added in Clojure 1.13 is a new 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.
372+
373+
[source,clojure]
374+
----
375+
(def small-map {:a 5})
376+
(let [{:keys [a b & :c], :as m, :or {a 42 b 3 :c "unknown"}, :select sel, :defaults df} small-map]
377+
(println sel))
378+
379+
;= {:c "unknown", :b 3, :a 5}
380+
----
381+
371382
Associative destructuring can be nested and combined with sequential destructuring as needed.
372383

373384
[source,clojure]

0 commit comments

Comments
 (0)