A higher-order function can do what this macro does: https://gist.github.com/favila/ecdd031e22426b93a78f
On Friday, June 10, 2016 at 1:07:58 PM UTC-5, Travis Daudelin wrote: > > Hi all! > > I'm current working on a project where I am ingesting events off a stream > and processing them. There are many many steps involved in the processing > component, so I am choosing to write the steps as a series of transducers > (because, hey, they're super cool!). Here's the problem though, after > writing the first 2 processing steps I'm noticing that all of them are > going to look very similar: > > (defn a-step-transducer > [] > (fn [reducing-fn] > (fn > ([] (reducing-fn)) > ([result] (reducing-fn result)) > ([[guid-state processed-events :as result] event] > ;; step-specific logic > )))) > > Given how many steps I am planning to write, this is a ton of boilerplate! > So, my first thought was to use a macro to abstract away all this > boilerplate. Now, I have to admit that Clojure is my first Lisp, so I'm > really not sure I fully understand when or why to use macros to do things. > My current understanding is that macros are a kind of "template" for code, > so something like this where I don't want to write the same function > structure over and over seems like a decent use case for macros (feel free > to correct me if I'm totally off on this). Here is my first attempt: > > (defmacro deftransducer > [body] > `(fn [reducing-fn] > (fn > ([] (reducing-fn)) > ([result] (reducing-fn result)) > ([[guid-state processed-events :as result] event] > ~@body)))) > > The idea here being that in body I can reference the variables defined by > the macro like reducing-fn, result, event, etc. Of course, I quickly > found out that this doesn't work: > > storm-etl.entry-processing> (deftransducer "something") > CompilerException java.lang.RuntimeException: Can't use qualified name as > parameter: storm-etl.entry-processing/reducing-function > > Some quick googling tells me that the solution to this is to use gensyms > for these variable names, but that would defeat the whole purpose of this > because I want to be able to reference those variables from within the code > that I pass to my macro. Is this an appropriate use case for macros or am I > way off base? Is there an alternative approach that would be recommended? > -- 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.