Hi,

On 4 Okt., 08:31, Meikel Brandmeyer <m...@kotka.de> wrote:
> 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 function "process-department" will be executed about 100.000 times
in one run. So I am trying to avoid macros and lamdas, and also
transfering as much information as possible into the structure of the
function to become faster. This is another reason for creating
customized functions by macros (hope this does not offend the meaning
of macros). Anyway, I will keep this solution at the back of my mind.

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

You've made my day! I always tried code like:

(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))))))

This would expand to the last statement, ignoring the first two. So I
started searching an "apply-macro" solution.

> Requiring eval is a code stink.
Agree.

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

This was my reason for starting this thread =)

> Hope this helps.

Yes, it does!

thx @all, Benjamin
--~--~---------~--~----~------------~-------~--~----~
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