That's an awesome solution Meikel!

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.

Thanks!
  -Patrick

On Feb 10, 2:09 am, Meikel Brandmeyer <m...@kotka.de> wrote:
> Whoops. Misclick. Sorry
>
> Here again:
>
> (def *deferred-bindings* {})
>
> (defmacro do-binding
>   [& body]
>   `(with-bindings *deferred-bindings*
>      ~@body))
>
> (defmacro bind-later
>   [bindings & body]
>   (let [vars   (map (fn [v] `(var ~v)) (take-nth 2 bindings))
>         values (take-nth 2 (next bindings))]
>     `(binding [*deferred-bindings* (merge *deferred-bindings*
>                                           ~(zipmap vars values))]
>        ~@body)))
>
> user=> (def a "outer a")
> #'user/a
> user=> (bind-later [a "inner a"] (println a) (do-binding (println a)))
> outer a
> inner a
> nil
> user=> (def b "outer b")
> #'user/b
> user=> (bind-later [a "inner a"] (println a) (bind-later [b "inner b"]
> (println b) (do-binding (println a) (println b)))
> )
> outer a
> outer b
> inner a
> inner b
> nil
>
> Nested invocations merge together. However I haven't checked all the
> edge cases.
>
> And always with binding: beware thread boundaries. But I think you
> know the drill. ;)
>
> Sincerely
> Meikel

-- 
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

Reply via email to