Hi,

Am 04.10.2009 um 00:50 schrieb b2m:

What macros do y'all have that you want to "apply" things to?

A sure code smell, IMO. It most likely is based on a misunderstanding what macros are capable to do.

I am using structs and functions for workings with these structs.

Just some very general example:

(defstruct department :name :head :members-l1 :members-l2 ...)

(defn process-department [department-struct]
 (reduce + (list
                   (* members-l1-fee (:members-l1 department-
struct))
                   (* members-l2-fee....)))

The functions themselves can be easily made independent from the number of keys. Just save the keys in a constant.

(defn process-department
  [department-struct]
  (->> +payment-levels+
    (map #(* (fee %) (department-struct %)))
    (reduce +)))

So you actually don't need specific functions for different companies. You just need some metadata on the struct.

The number of "payment levels" are not the same for every "company",
so I thought of creating these structs and functions by macros.
Because they all have the same arguments these calls are wrapped in a
function:

(defn init-funs [name & levels]
  (do
   (apply-macro create-department-struct name levels)
   (apply-macro create-process-department name levels)
   nil))

Here we have the smell! You cannot define functions with a function. You have to use a macro! It would look like this:

(defmacro init-department
  [name & levels-and-fees]
  (let [fee (apply hash-map levels-and-fees)]
     `(do
       (def ~'fee ~fee)
       (defstruct ~name ~@(keys fee))
       (def ~'payment-levels ~(set (keys fee))))))

Note, the "apply-macro" in form of ~...@.

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.

Requiring eval is a code stink. It is a sign, that some pieces of the puzzle don't really fit together. Whenever you arrive at the need of eval in "normal" code, you should step back and look where the misunderstanding is.

Hope this helps.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to