1

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.

Alper
  • 3,424
  • 4
  • 39
  • 45
  • 1
    This is explicitly documented in https://clojure.org/guides/destructuring; the question itself is a subset of https://stackoverflow.com/questions/49375198/destructuring-argument-in-clojure (I'd close it as duplicate, but arguably, the *other* question should be closed as being too-broad, cramming several questions into one). – Charles Duffy Dec 28 '19 at 22:19

1 Answers1

2

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441