Really sorry about pasting the code in the email. It seems to have added 2-3 extra new-lines after every line .. :( .. but the same code is present in the github-gist. Sunil.
On Mon, Dec 6, 2010 at 10:57 AM, Sunil S Nandihalli < sunil.nandiha...@gmail.com> wrote: > Hello everybody, > The following gist has the code I wrote to learn writing macros. > https://gist.github.com/729915 > > > (let [level (atom 0)] > > > > (defmacro inc-level [] (swap! level inc) nil) > > > > (defmacro dec-level [] (swap! level dec) nil) > > > > (defmacro with-separator [& body] > > > > `(do > > > > (inc-level) > > > > (println) > > > > (println ~(str "----------------------------start-" @level > "-----------------------")) > > > > (let [x# (do ~...@body)] > > > > (println ~(str "------------------------------end-" @level > "-----------------------")) > > > > (dec-level) > > > > x#)))) > > > > > (let [*stack-of-local-binding-maps* (atom '())] > > > > (defmacro pop-environment [] (swap! *stack-of-local-binding-maps* pop) nil) > > > > (defmacro push-environment [] (swap! *stack-of-local-binding-maps* conj > &env) nil) > > > > (defmacro display-new-local-bindings [& body] > > > > (let [new-locals (->> (clojure.set/difference (set &env) > > > > (set (first > @*stack-of-local-binding-maps*))) > > > > (into {}) keys) > > > > print-statements (map (fn [x] `(println ['~x ~x])) new-locals)] > > > > (if (not-empty new-locals) > > > > `(with-separator (push-environment) ~...@print-statements ~...@body > (pop-environment)) > > > > `(do ~...@body))))) > > > > > > There are two macro which are using some kind of stack.. which are using > (push-pop) or (inc-dec) pairs of macros which need to modify state while the > compiler is running and expand into nil. I know this is not good style. I > would like some feedback as to how I can make this more idiomatic. > > > > executing the following code > > isomorphism.send> (display-new-local-bindings > (let [x 10] > (display-new-local-bindings > (let [y 20] > (display-new-local-bindings (+ x y)))))) > ----------------------------start-0----------------------- > [x 10] > ----------------------------start-1----------------------- > [y 20] > ------------------------------end-1----------------------- > ------------------------------end-0----------------------- > nil > isomorphism.send> > > > > as shown above is displaying the new-local-bindings (when compared to the > previous call to display-new-local-bindings) with a nesting-level numbered > separators. How can write it without using atoms and swaps...? > > > Thanks, > Sunil. > > -- 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