Aw: defnk pre- and post-conditions

2011-04-26 Thread Meikel Brandmeyer
Hi,

clojure supports keyword arguments for a quote some time now.

(defn foo
  [positional1 ... positionalN  {:keys [kw1 ... kwn]}]
  ...)

The syntax is the usual destructuring syntax for maps. So you can specify 
defaults via :or etc. And even have none keyword keys.

Sincerely
Meikel

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

Re: Aw: defnk pre- and post-conditions

2011-04-26 Thread Hugo Duncan

On Tue, 26 Apr 2011 07:27:52 -0400, Meikel Brandmeyer m...@kotka.de wrote:


clojure supports keyword arguments for a quote some time now.

(defn foo
  [positional1 ... positionalN  {:keys [kw1 ... kwn]}]
  ...)


This is used extensively in Pallet but has a couple of drawbacks.

  - Repetition of keywords to specify defaults
  - Repetition of keywords and defaults in doc strings
  - The defaulted argument values are not available as a map
  - No flag for presence of specified keyword (à la CL)

Inspired by a discussion [1] on supporting shFlags in stevedore, I drafted  
a defnkw [2] that allows for the following syntax:


(defnkw foo
  My foo
  [positional1 … positionalN
 [[kw1 description1 default1]
   [kw2 description2]]]
  …)

and automatically generates a suffix to the doc string.

(doc foo) =
My foo
- kw1 description1. Default default1.
- kw2 description2.

The formatting of the generated doc string can obviously be improved, and  
it is not obvious to me what syntax to use for naming the keyword option  
map (:as). This approach would allow defaulted values to be merged back  
into the option map.  Support for presence flags would require an extra  
element in the specification vector (e.g. [description default flag?])


An alternative would be to use a more defnk like syntax:

(defnkw foo
  My foo
  [positional1 … positionalN
 :kw1 [description1 default1]
  :kw2 description2]
  …)


I would be interested in any comments.


[1] https://github.com/pallet/stevedore/pull/1
[2]  
https://github.com/pallet/common/blob/feature%2Fdefn-for-kw-args/src/pallet/common/core.clj

--
Hugo Duncan

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


Re: Aw: defnk pre- and post-conditions

2011-04-26 Thread Ambrose Bonnaire-Sergeant
On Tue, Apr 26, 2011 at 8:26 PM, Hugo Duncan duncan.h...@gmail.com wrote:

 Inspired by a discussion [1] on supporting shFlags in stevedore, I drafted
 a defnkw [2] that allows for the following syntax:

 (defnkw foo
  My foo
  [positional1 … positionalN
 [[kw1 description1 default1]
   [kw2 description2]]]
  …)



 The formatting of the generated doc string can obviously be improved, and
 it is not obvious to me what syntax to use for naming the keyword option map
 (:as). This approach would allow defaulted values to be merged back into the
 option map.  Support for presence flags would require an extra element in
 the specification vector (e.g. [description default flag?])


 An alternative would be to use a more defnk like syntax:

 (defnkw foo
  My foo
  [positional1 … positionalN
 :kw1 [description1 default1]
  :kw2 description2]
  …)


Here's an interesting alternative:

(defnkw foo
 My foo
 [positional1 ... positionalN
   {:kw1 {:doc The kw1 argument
   :default default1}
 :kw2 {:doc Another
   :default blah
   :as wheee}
 :kw2 {}} :as options]

We can specify an :as key on the entire option map.

Maps are more flexible than vectors for attributes. :doc, :default, :as are
fairly descriptive
and other attributes can easily be added. There would be defaults for each
key.

I've put a spin on the CL defun shortcut for renaming keyword args (:kw2
will be bound to wheee).
See Keyword Parameters: http://www.gigamonkeys.com/book/functions.html

This is an alternative which is much closer to CL's method:
 {[:kw2 wheee] {... atrs.. ]}

On the whole it seems pretty readable, but may be misleading with the { }
syntax.

It's also markedly more verbose.


 I would be interested in any comments.


 [1] https://github.com/pallet/stevedore/pull/1
 [2]
 https://github.com/pallet/common/blob/feature%2Fdefn-for-kw-args/src/pallet/common/core.clj
 --
 Hugo Duncan


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


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