Saturday, 31 March 2012

Final thing on destructuring.

I haven't been writing in some time, that is because I haven't been doing much coding lately. Now I've started again, and immediately found something interesting.

So, I managed to nuke my environment (Emacs/Slime/Swank) by trying to install a Common Lisp thingie next to it, so I had to setup everything again and... it turned out my previous setup was unnecessarily complicated and hacky. Oh well.

Anyways, I've been expanding a few macros for fun to see how exactly they work (->, to be exact), and expanded let by accident.

So... one quick call to (doc let) later...

01(defmacro let
02  "binding => binding-form init-expr
03
04  Evaluates the exprs in a lexical context in which the symbols in
05  the binding-forms are bound to their respective init-exprs or parts
06  therein."
07  {:added "1.0", :special-form true, :forms '[(let [bindings*] exprs*)]}
08  [bindings & body]
09  (assert-args let
10     (vector? bindings) "a vector for its binding"
11     (even? (count bindings)) "an even number of forms in binding vector")
12  `(let* ~(destructure bindings) ~@body))


Actually, this is taken from clojuredocs.org. Neat site. But I found it out over (doc let).
It turns out let is NOT a special form, let* is. And then...

(destructure bindings)

Yes. Destructuring is actually implemented in the language. clojure.core/destructure, to be exact. You can even pass quoted argument vectors to that function and see the output.

The source is ugly (and long) as hell, but still, it's fascinating this is implemented in the language as well.



















No comments: