On Mon, Feb 7, 2011 at 10:06 PM, CuppoJava <patrickli_2...@hotmail.com> wrote: > I've thought about that actually. And it wouldn't work. > > So (defn destructure [form value] > ...magic...) > > (defmacro let [forms body] > `(let* ~(vec (destructure forms body)) <- at this point, "body" is > not known yet. It's just a symbol. > ~@body) > > This approach won't work because "body" is only a symbol within the > macro. We don't know it's value until runtime.
What? While the macro is executing "body" will be bound to an s-expression (basically, source code parsed to an AST but no further; source code as processed by the reader). What was being discussed is easy in principle to accomplish. As it stands we have something like (defmacro let [binding-form body] `(let* ~(some-complex-code-that-emits-a-vector binding-form) ~@body)) already; all that's needed is to lift "some-complex-code-that-emits-a-vector" out to an explicit function with binding-form as the sole argument, whose output is a vector of alternating names and sexps. This will then do as a general, runtime-usable destructure function that turns vectors resembling binding forms into flattened vectors. You can get the desired maps with a simple additional process of (reduce (fn [a [b c]] (assoc a b c)) {} (partition 2 the-vector)). (Maybe (into {} (partition 2 the-vector)) will DTRT with repeated keys, too.) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en