How to replace code inside a macro
I wanted to achive following inside a macro. Lets say I have a macro called deffilter which can use in following manner. (deffilter split-sentence ["sentence"] ["word"] (work {} (let [tuple (pop) words (s/split (value-at tuple 0) " ")] (doseq [w words] (push [w] :anchor tuple) I need to transform above work section to something looks like following. (work [tuple1234] (let [tuple tuple1234 words (s/split (value-at tuple 0) " ")] (doseq [w words] (push [w] :anchor tuple (pop) should be replaced by tuple1234 like string which is same as parameter to work function. Really appriciate if someone can point me a way to achieve this. I am not sure having pop as another macro is viable because I need to use the same name as input parameter to work function. Thanks Milinda -- 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/groups/opt_out.
Re: How to replace code inside a macro
Can you use macrolet from here: https://github.com/clojure/tools.macro ? On Tue, Mar 4, 2014 at 11:30 AM, milinda wrote: > I wanted to achive following inside a macro. Lets say I have a macro > called deffilter which can use in following manner. > > (deffilter split-sentence ["sentence"] ["word"] >(work {} > (let [tuple (pop) >words (s/split (value-at tuple 0) " ")] >(doseq [w words] > (push [w] :anchor tuple) > > I need to transform above work section to something looks like following. > > (work [tuple1234] >(let [tuple tuple1234 > words (s/split (value-at tuple 0) " ")] > (doseq [w words] > (push [w] :anchor tuple > > (pop) should be replaced by tuple1234 like string which is same as > parameter to work function. > > Really appriciate if someone can point me a way to achieve this. I am not > sure having pop as another macro is viable because I need to use the same > name as input parameter to work function. > > Thanks > Milinda > > -- > 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/groups/opt_out. > -- 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/groups/opt_out.
Re: How to replace code inside a macro
Thanks Jason. I'll try this. Milinda On Tuesday, March 4, 2014 1:35:39 PM UTC-5, Jason Felice wrote: > > Can you use macrolet from here: https://github.com/clojure/tools.macro ? > > > On Tue, Mar 4, 2014 at 11:30 AM, milinda > > wrote: > >> I wanted to achive following inside a macro. Lets say I have a macro >> called deffilter which can use in following manner. >> >> (deffilter split-sentence ["sentence"] ["word"] >>(work {} >> (let [tuple (pop) >>words (s/split (value-at tuple 0) " ")] >>(doseq [w words] >> (push [w] :anchor tuple) >> >> I need to transform above work section to something looks like following. >> >> (work [tuple1234] >>(let [tuple tuple1234 >> words (s/split (value-at tuple 0) " ")] >> (doseq [w words] >> (push [w] :anchor tuple >> >> (pop) should be replaced by tuple1234 like string which is same as >> parameter to work function. >> >> Really appriciate if someone can point me a way to achieve this. I am not >> sure having pop as another macro is viable because I need to use the same >> name as input parameter to work function. >> >> Thanks >> Milinda >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com . >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- 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/groups/opt_out.
Re: How to replace code inside a macro
I tried this. But couldn't achieve what I wanted. Below is the macro I wrote by utilizing macro let (defmacro [body] (let [x (gensym 'tu) defpop-decl (list 'defpop [] x)] `(macrolet [~defpop-decl] (defn execute [~x] ~body I wanted macro to work like following. ( (let [x 10] (+ x (defpop This should generate function like (defn execute [tu3455] (let [x 10] (+ x tu3455))) But above macro complain saying "CompilerException java.lang.RuntimeException: Unable to resolve symbol: tu3455 in this context. Looks like I am doing something wrong or I am not understanding how clojure read, evaluation and macros work. Please let me know if anyone know way to achieve what I want using macros and macrolet. Thanks Milinda On Tuesday, March 4, 2014 2:28:06 PM UTC-5, milinda wrote: > > Thanks Jason. I'll try this. > > Milinda > > On Tuesday, March 4, 2014 1:35:39 PM UTC-5, Jason Felice wrote: >> >> Can you use macrolet from here: https://github.com/clojure/tools.macro ? >> >> >> On Tue, Mar 4, 2014 at 11:30 AM, milinda wrote: >> >>> I wanted to achive following inside a macro. Lets say I have a macro >>> called deffilter which can use in following manner. >>> >>> (deffilter split-sentence ["sentence"] ["word"] >>>(work {} >>> (let [tuple (pop) >>>words (s/split (value-at tuple 0) " ")] >>>(doseq [w words] >>> (push [w] :anchor tuple) >>> >>> I need to transform above work section to something looks like following. >>> >>> (work [tuple1234] >>>(let [tuple tuple1234 >>> words (s/split (value-at tuple 0) " ")] >>> (doseq [w words] >>> (push [w] :anchor tuple >>> >>> (pop) should be replaced by tuple1234 like string which is same as >>> parameter to work function. >>> >>> Really appriciate if someone can point me a way to achieve this. I am >>> not sure having pop as another macro is viable because I need to use the >>> same name as input parameter to work function. >>> >>> Thanks >>> Milinda >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Clojure" group. >>> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- 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/groups/opt_out.
Re: How to replace code inside a macro
--On 4 Mar 2014 23:07:27 -0800 milinda wrote: I wanted macro to work like following. ( (let [x 10] (+ x (defpop This should generate function like (defn execute [tu3455] (let [x 10] (+ x tu3455))) Try this: (defmacro [body] `(macrolet [(~'defpop [] 'tu#)] (defn execute [tu#] ~body))) In action: user=> (macroexpand-1 '( (let [x 10] (+ x (defpop) (clojure.tools.macro/macrolet [(defpop [] (quote tu__818__auto__))] (clojure.core/defn user/execute [tu__818__auto__] (let [x 10] (+ x (defpop) user=> (macroexpand '( (let [x 10] (+ x (defpop) (do (def user/execute (fn* ([tu__818__auto__] (let* [x 10] (+ x tu__818__auto__)) Konrad. -- 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/groups/opt_out.
Re: How to replace code inside a macro
Thanks Konrad. Your unquoting trick worked. But I am not exactly sure how to reason about these types of situations. Can you please shed some lights behind the logic of above unquoting if possible. Thanks Milinda On Wednesday, March 5, 2014 8:33:03 AM UTC-5, Konrad Hinsen wrote: > > --On 4 Mar 2014 23:07:27 -0800 milinda > > wrote: > > > I wanted macro to work like following. > > > > > > ( (let [x 10] (+ x (defpop > > > > > > This should generate function like > > > > > > (defn execute [tu3455] (let [x 10] (+ x tu3455))) > > Try this: > > (defmacro [body] > `(macrolet [(~'defpop [] 'tu#)] (defn execute [tu#] ~body))) > > In action: > > user=> (macroexpand-1 '( (let [x 10] (+ x (defpop) > (clojure.tools.macro/macrolet [(defpop [] (quote tu__818__auto__))] > (clojure.core/defn user/execute [tu__818__auto__] (let [x 10] (+ x > (defpop) > > user=> (macroexpand '( (let [x 10] (+ x (defpop) > (do (def user/execute (fn* ([tu__818__auto__] (let* [x 10] (+ x > tu__818__auto__)) > > > Konrad. > > -- 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/groups/opt_out.
Re: How to replace code inside a macro
Milinda if you are asking about the ~' that simply allows you to quote a symbol without the namespace resolution that a backquote ` provides. If you are trying to capture a local lexical value or otherwise do not want the full namespace resolved for the symbol, then you must use an ordinary quote on the symbol since part of the job of a backquote is to provide the namespace resolution. On Thursday, March 6, 2014 1:37:24 AM UTC+8, milinda wrote: > > Thanks Konrad. Your unquoting trick worked. But I am not exactly sure how > to reason about these types of situations. Can you please shed some lights > behind the logic of above unquoting if possible. > > Thanks > Milinda > > On Wednesday, March 5, 2014 8:33:03 AM UTC-5, Konrad Hinsen wrote: >> >> --On 4 Mar 2014 23:07:27 -0800 milinda wrote: >> >> > I wanted macro to work like following. >> > >> > >> > ( (let [x 10] (+ x (defpop >> > >> > >> > This should generate function like >> > >> > >> > (defn execute [tu3455] (let [x 10] (+ x tu3455))) >> >> Try this: >> >> (defmacro [body] >> `(macrolet [(~'defpop [] 'tu#)] (defn execute [tu#] ~body))) >> >> In action: >> >> user=> (macroexpand-1 '( (let [x 10] (+ x (defpop) >> (clojure.tools.macro/macrolet [(defpop [] (quote tu__818__auto__))] >> (clojure.core/defn user/execute [tu__818__auto__] (let [x 10] (+ x >> (defpop) >> >> user=> (macroexpand '( (let [x 10] (+ x (defpop) >> (do (def user/execute (fn* ([tu__818__auto__] (let* [x 10] (+ x >> tu__818__auto__)) >> >> >> Konrad. >> >> -- 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/groups/opt_out.
Re: How to replace code inside a macro
Hi, Milinda. I'll try to explain my reasoning of the situation. I always find it helpful to macroexpand if I'm having trouble: ;; original version user=> (macroexpand-1 '( boby)) (clojure.tools.macro/macrolet [(defpop [] tu1282)] ;; <- undef. lookup (clojure.core/defn user/execute [tu1282] boby)) ;; Konrad's version user=> (macroexpand-1 '(-kh body)) (clojure.tools.macro/macrolet [(defpop [] (quote tu__1294__auto__))] ;; <- (clojure.core/defn user/execute [tu__1294__auto__] body)) So, I think what is happening here is that the 'defpop' macro defined by macrolet needs to return a piece of code (like all macros). Konrad's version returns a symbol, so that's fine. But the original version is trying to look up the piece of code stored in the var tu1282 and return it. But that variable doesn't exist, so you get the CompilerException. Here is a slight modification of your original that explicitly does what Konrad did with auto-gensyms: (defmacro 3 [body] (let [x (gensym 'tu) defpop-decl (list 'defpop [] (list 'quote x))] ;; <- return the quoted symbol, no lookup `(macrolet [~defpop-decl] (defn execute [~x] ~body Hope that helps, Leif On Wednesday, March 5, 2014 12:37:24 PM UTC-5, milinda wrote: > > Thanks Konrad. Your unquoting trick worked. But I am not exactly sure how > to reason about these types of situations. Can you please shed some lights > behind the logic of above unquoting if possible. > > Thanks > Milinda > > On Wednesday, March 5, 2014 8:33:03 AM UTC-5, Konrad Hinsen wrote: >> >> --On 4 Mar 2014 23:07:27 -0800 milinda wrote: >> >> > I wanted macro to work like following. >> > >> > >> > ( (let [x 10] (+ x (defpop >> > >> > >> > This should generate function like >> > >> > >> > (defn execute [tu3455] (let [x 10] (+ x tu3455))) >> >> Try this: >> >> (defmacro [body] >> `(macrolet [(~'defpop [] 'tu#)] (defn execute [tu#] ~body))) >> >> In action: >> >> user=> (macroexpand-1 '( (let [x 10] (+ x (defpop) >> (clojure.tools.macro/macrolet [(defpop [] (quote tu__818__auto__))] >> (clojure.core/defn user/execute [tu__818__auto__] (let [x 10] (+ x >> (defpop) >> >> user=> (macroexpand '( (let [x 10] (+ x (defpop) >> (do (def user/execute (fn* ([tu__818__auto__] (let* [x 10] (+ x >> tu__818__auto__)) >> >> >> Konrad. >> >> -- 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/groups/opt_out.
Re: How to replace code inside a macro
--On 5 Mar 2014 09:37:24 -0800 milinda wrote: Thanks Konrad. Your unquoting trick worked. But I am not exactly sure how to reason about these types of situations. Can you please shed some lights behind the logic of above unquoting if possible. I suppose you refer to the ~'defpop, right? That is indeed a bit subtle. An unquote followed by a quote should cancel, one might think. The difference comes from namespacing: `a resolves a in the current namespace, whereas `~'a returns a symbol without a namespace. This matters because macrolet (just like plain let) replaces only non-namespaced symbols. Konrad. -- 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/groups/opt_out.
Re: How to replace code inside a macro
Thank you everyone for explanations. I think now I understand macros and how to use macro let better. Milinda On Thursday, March 6, 2014 1:51:59 PM UTC-5, Konrad Hinsen wrote: > > --On 5 Mar 2014 09:37:24 -0800 milinda > > wrote: > > > Thanks Konrad. Your unquoting trick worked. But I am not exactly sure > how > > to reason about these types of situations. Can you please shed some > > lights behind the logic of above unquoting if possible. > > I suppose you refer to the ~'defpop, right? That is indeed a bit subtle. > An > unquote followed by a quote should cancel, one might think. The difference > comes from namespacing: `a resolves a in the current namespace, whereas > `~'a returns a symbol without a namespace. This matters because macrolet > (just like plain let) replaces only non-namespaced symbols. > > Konrad. > > -- 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.