Please look at how fns are generated in http.async.client http://github.com/neotyk/http.async.client/blob/master/src/http/async/client/util.clj#L54
this macro is later used here: http://github.com/neotyk/http.async.client/blob/master/src/http/async/client.clj#L43
HTH,
Hubert.
> --
> 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
(defmacro ultra-synth [prefix keywords]
(let [symbols (map (comp symbol name) keywords)
fn-names (map (comp symbol
(partial str prefix) name) keywords)
defn-forms (map
(fn [fn-name symbol]
`(defn ~fn-name [n#]
(= n# ~symbol)))
fn-names symbols)]
`(do ~@defn-forms)))
thunderbolt.play> (def a 5)
#'thunderbolt.play/a
thunderbolt.play> (def b 5)
#'thunderbolt.play/b
thunderbolt.play> (ultra-synth "hi" [:a :b])
#'thunderbolt.play/hib
thunderbolt.play> (hia 5)
true
--Robert McIntyre
--Robert McIntyre
On Fri, Sep 10, 2010 at 8:24 PM, icemaze <ice...@gmail.com> wrote:
> I agree: eval never looks pretty.
>
--Robert McIntyre
It is considered bad because eval is for dynamic evaluation. In this
case you use it at macro time, which shouldn't be necessary most of
the time.
(defn my-complex-function [] ....)
(defmacro insert-the-result-of-the-complex-function []
(my-complex-function))
In your case, as often for strange macro, first write a function that
does the job:
(defn extract-keyword-and-generate-code [].....)
(defmacro generate-methods []
`(do
~@(extract-keyword-and-generate-the-code)))
If you have to use an eval, I find it nicer to use it to compute the
code, not to actually define things.
(defmacro generate-methods [expr]
`(do
~@(extract-keyword-and-generate-the-code (eval expr))))
That way you can often work the eval out, by limiting the kind of
possible exprs.
(If it is only a symbol or a list of keywords, for example)
Anyway, that's just a suggestion. Your solution works too.
Best,
Nicolas.