Alper
Alper

Reputation: 3953

Specifying bindings syntax with :as

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

Answers (1)

Charles Duffy
Charles Duffy

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) and x is bound to the character \C, and remaining 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

Related Questions