Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion defnk addition to c.c.def
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Meikel Brandmeyer  
View profile  
 More options Jun 4 2009, 6:06 pm
From: Meikel Brandmeyer <m...@kotka.de>
Date: Fri, 5 Jun 2009 00:06:27 +0200
Local: Thurs, Jun 4 2009 6:06 pm
Subject: defnk addition to c.c.def

Hello Stephen,

the following is a macro defining a function with keyword
arguments. It is called like this:

(defnk foo
   [a b c :x 1 :z 2]
   (println x z))

a, b and c denote positional arguments, 1 and 2 are default
values for x resp. z. This pattern came up several times now,
and there were always some "aha" effects. So it may be a
nice addition to c.c.def.

Here's the macro:
(defmacro defnk
   "Define a function accepting keyword arguments. Symbols up to the  
first
   keyword in the parameter list are taken as positional arguments.  
Then
   an alternating sequence of keywords and defaults values is  
expected. The
   values of the keyword arguments are available in the function body by
   virtue of the symbol corresponding to the keyword (cf. :keys  
destructuring).
   defnk accepts an optional docstring as well as an optional metadata  
map."
   [fn-name & fn-tail]
   (let [[fn-name [args & body]] (name-with-attributes fn-name fn-tail)
         [pos kw-vals]           (split-with symbol? args)
         kw-vals                 (apply hash-map kw-vals)
         de-map                  {:keys (vec (keys kw-vals))
                                  :or   kw-vals}]
     `(defn ~fn-name
        [~@pos & options#]
        (let [~de-map (apply hash-map options#)]
          ~@body))))

The docstring is a bit contorted but I'm too sleepy now,
to get that right...

Sincerely
Meikel

  smime.p7s
5K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.