> As an aside, this doesn't look like a good use-case for macros
>. You'd probably be better defining a map with keys for :
> worker, :channel, etc. Either that or a protocol.
 
Thank you much for your help. But I thought this is exactly what macros 
were for? In another language I would end up just copying-and-pasting the 4 
defs, and then changing "update" to "remove" so I could have both an update 
channel and a remove channel. I thought that macros were suppose to be good 
at removing that kind of redundancy? 




On Tuesday, December 10, 2013 12:53:31 PM UTC-5, James Reeves wrote:
>
> Remember that only the last form will be returned. So:
>
>     (defn foo [] 1 2 3 4)
>
> will always return 4. The same principle applied with your macro. You 
> define four forms, but only return the last one. Instead, try wrapping all 
> the forms in a "do" block.
>
> As an aside, this doesn't look like a good use-case for macros. You'd 
> probably be better defining a map with keys for :worker, :channel, etc. 
> Either that or a protocol.
>
> - James
>
>
> On 10 December 2013 17:42, larry google groups 
> <lawrenc...@gmail.com<javascript:>
> > wrote:
>
>> I am working on web software where admins will be using HTML forms to 
>> update data in a MongoDb database. I decided to use Lamina to off-load 
>> the work to the background. There are several operations that need to 
>> happen: updates, deletions, etc, and I thought I'd put each on a different 
>> channel. So I tried to write a macro to generate the code needed for each 
>> of those channels. This was my first attempt: 
>>
>> (defmacro establish-channel [action noun to-execute start-function-name]
>>   (let [channel-name (symbol (str "documents-to-" action "-in-" noun 
>> "-channel"))
>>         additive-action-name (symbol (str "add-to-" action "-channel"))
>>         worker-name (symbol (str action "-" noun "-worker"))]
>>
>>   `(def ~channel-name (channel))
>>
>>   `(defn ~additive-action-name [document]
>>      (enqueue ~channel-name document))
>>
>>   `(defn ~worker-name []
>>      (loop [document @(read-channel ~channel-name)]
>>        (~to-execute document)
>>        (recur @(read-channel ~channel-name))))
>>   
>>   `(defn ~start-function-name []
>>      (future (~worker-name))
>>      (future (~worker-name))
>>      (future (~worker-name))
>>      (future (~worker-name))
>>      (future (~worker-name)))))
>>
>>
>> But in the repl, when I run this, and then I do this:
>>
>> user> (macroexpand-1 (establish-channel update mongo println 
>> start-mongo-channel))
>>
>> I get: 
>>
>> CompilerException java.lang.RuntimeException: Unable to resolve symbol: 
>> update-mongo-worker in this context, compiling:(NO_SOURCE_PATH:1:16) 
>>
>> It seems to be complaining about this line:
>>
>>      (future (~worker-name))
>>
>> But I defined that function just above that line: 
>>
>>   `(defn ~worker-name []
>>
>> So what could the problem be? 
>>
>>  -- 
>> -- 
>> 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<javascript:>
>> 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 <javascript:>
>> 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 <javascript:>.
>> 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.

Reply via email to