On Fri, Jun 17, 2011 at 2:31 PM, Tassilo Horn <tass...@member.fsf.org> wrote:
> Ken Wesson <kwess...@gmail.com> writes:
>
> Hi Ken!
>
>> (defmacro defnm [name argvec & body]
>>   `(defn ~name ~argvec
>>      (let ~(vec
>>              (apply concat
>>                (for [a argvec :when (and (map? a) (:or a) (:as a))]
>>                  [(:as a) `(merge ~(keysm (:or a)) ~(:as a))])))
>>        ~@body)))
>
> Hey, that's pretty nice. :-)

Thanks.

>> 2. This bare-bones macro doesn't handle docstrings on the functions.
>
> That's bad.  But for my use-case, the docstrings are mandatory, so I can
> easily add them to your macro.

It's easy to have the macro deal with both docstrings and their
absence, in principle. But you need to take any number of arguments,
skip until you get a vector or a list and just generate (defn ...)
with the earlier arguments verbatim, and if the next argument is a
vector do what defnm did with the vector and body, and otherwise, do
that inside *each remaining argument* as it will be a ([arity one]
(body one)) ([arity the second] (body two)) ... kind of deal.

It complicates things but it's not impossible or even difficult, just
tedious. :) And it only has to be done once; then you can use the
macro pretty generally.

Your :giving idea is good, too, and the macro could be modified for
that quite easily just by changing the first two (:as a)s (not the one
in the merge) to (:giving a)s and dropping (:or a) from the
conditional (so :giving will work without :or being present). Then you
could use :as and :giving in the same instance, or just one, or the
other, and the appropriate symbols will be bound in the body.
(Fortunately (merge nil {:some map}) doesn't blow up so the merge will
work even without an :or key.)

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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