On Sat, Oct 3, 2009 at 6:50 PM, b2m <b2monl...@googlemail.com> wrote:

> > What macros do y'all have that you want to "apply" things to?
> (defn init-funs [name & levels]
>   (do
>    (apply-macro create-department-struct name levels)
>    (apply-macro create-process-department name levels)
>    nil))
>
> A call like
> (init-funs "company1" "level1" "level2")
> will result in
> (defstruct company1-department :name :head :level1 :level2)
> and so on.
>
> So I need macros for the benefit of individual strucs and functions
> for every "company", and I need something like apply to pass
> parameters from the inital function to the macros. This is working by
> using (eval (apply-macro... as described in a previous posting in this
> thread. It would be nice to have a solution without eval. But maybe my
> attempt of writing code that writes code is too enthusiastic.


Not really. But since you're automating generation of deffoos (in this
example, defstructs) you can, and should, use macros all the way down. I.e.
init-funs can, and should, be a macro above and it can then just be:

(defmacro init-funs [name & levels]
  `(do
    (create-department-struct ~name ~...@levels)
    (create-process-department ~name ~...@levels)
    nil))

or similarly as the case may be.

If you need to be creating these things dynamically, with information only
available at runtime, defstruct is probably the wrong tool for the job, or
the only struct member should be :name, and the levels at least should just
be ordinary map keys (not struct keys); then you can dispense with init-funs
entirely, and replace the call currently used to get a new instance of the
struct with a call that given a name, figures out the appropriate levels
(from a name to level-name-list hashmap you maintain at runtime) and
initializes a structmap with the :name struct-key set appropriately and also
empty lists for each level-name key appropriate for that name.

Or something like that.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to