Reputation: 3953
Say I have a binding such as:
loop [[x & more :as tree] tree, minVal 99, maxVal -99]
What does :as
do? I have tried looking around but can't find anything about it.
Upvotes: 1
Views: 54
Reputation: 295325
There's an explicitly-on-point example in https://clojure.org/guides/destructuring --
(def word "Clojure") (let [[x & remaining :as all] word] (apply prn [x remaining all])) ;= \C (\l \o \j \u \r \e) "Clojure"
Here
all
is bound to the original structure (String, vector, list, whatever it may be) andx
is bound to the character\C
, andremaining
is the remaining list of characters.
As this demonstrates, :as
binds the original, pre-destructuring value, thus in the example of your loop making tree
retain its original value. That's not particularly useful when that value already has a name, but is very useful when it itself is a return value or otherwise as-yet-unnamed.
Upvotes: 2