I just started to play  with macros in Clojure.  The following is a simple 
example that expands to:

(on-message "topic-test" (clojure.core/fn [topic13730] (println topic13730 
"test")))

To make this work I had to replace the symbol 'topic in the body with the 
gensym symbol. Is this the right way to do this ?

Regards
  Roger

Code:

(defn on-message [topic callback]
  (callback
       topic))

(defmacro listen
  [topic expression]
   (let [arg (gensym 'topic)
         body (map (fn [el] (if (= el 'topic) arg el)) expression)]
   `(on-message ~topic
     (fn [~arg] (~@body)))))

(listen "topic-test" (println topic "test"))

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to