Hi,
On 10 Feb., 14:58, CuppoJava <[email protected]> wrote:
> That's an awesome solution Meikel!
You are welcome. :)
> I forgot to specify that I want the bindings to have lexical scope
> only. But I think I can get that to work easily by modifying your
> solution.
Haha. In fact that was my first impulse. But then I saw the (def a)
and read "bind" and changed my interpretation to dynamic scope.
Lexical scope is much harder. The only "simple" solution, I see at the
moment, is walking the body of bind-later and do substitution. This is
in general ugly, but the only thing I can think of at the moment. This
is a (very) rough sketch, but you should get the key idea.
(declare do-bind)
(defn magic-walk-fn
[form locals db env]
(if (seq? form)
(let [[f & args] form
args (map magic-walk-fn args)]
(if (when-not (contains? env f)
(= #'do-bind (resolve f)))
`((fn [~locals] ~@args) ~db)
`(~f ~@args)))
(else-do-other-walking-magic form)))
(defmacro bind-later
[bindings & body]
(let [db (gensym "deferred-bindings")
locals (vec (take-nth 2 bindings))
values (vec (take-nth 2 (next bindings)))]
`(let [~db ~values]
~@(map #(magic-walk-fn % locals db &env) body))))
Note that do-bind is just a dummy to identify the correct forms to
transform.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en