SPOILER BELOW. I'm not sure how much help you want, so I went ahead and wrote your macro. Whitespace padding so that you won't see it if you don't want the whole solution:
user=> (defmacro make-fn [key] (let [sym (->> key name (str "synthetic-") symbol)] `(defn ~sym [n#] (= n# ~key)))) #'user/make-fn user=> (make-fn :a) #'user/synthetic-a user=> (synthetic-a :q) false user=> (synthetic-a :a) true user=> (macroexpand '(make-fn :a)) (def synthetic-a (.withMeta (clojure.core/fn synthetic-a ([n__1041__auto__] (clojure.core/= n__1041__auto__ :a))) (.meta (var synthetic-a)))) On Sep 10, 1:02 pm, Alan <a...@malloys.org> wrote: > I actually did this just the other day, to create a simple C-style > enum macro (I assume someone has a better version; this was a learning > exercise). You can see my project atwww.github.com/amalloy/enum. It > sounds like your problem might be constructing the symbol to define; > the solution will look something like: > > (let [fn-sym (->> name (str "prefix-") symbol)] > `(defn ~fn-sym [args] body)) > > On Sep 10, 12:39 pm, icemaze <icem...@gmail.com> wrote: > > > Hi, I'm developing a small DSL with Clojure and I need to define many > > similar functions. I'd like to do that programmatically, of course. > > > My solution (involving a simple macro) doesn't work, so I won't bother > > you with it. I'll post it if anyone asks. > > > Basically what I need is: given a list of keywords, for each keyword x > > i need to define a function whose name is (str "prefix-" (name x)). > > The function has a very short body which uses x in one place > > (something like (fn [n] (= n x))). > > > Can you help me please? I've been banging my head against this for > > over four hours and I'm getting a little frustrated. > > > Thanks. > > -- 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