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
defnk addition to c.c.def
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
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.
Meikel Brandmeyer  
View profile  
 More options Jun 4 2009, 6:22 pm
From: Meikel Brandmeyer <m...@kotka.de>
Date: Fri, 5 Jun 2009 00:22:42 +0200
Local: Thurs, Jun 4 2009 6:22 pm
Subject: Re: defnk addition to c.c.def

Hi again,

Am 05.06.2009 um 00:06 schrieb Meikel Brandmeyer:

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

And of course I'm too sleepy to miss the keyword to
symbol conversion....

(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)
         syms                    (map #(-> % name symbol) (take-nth 2  
kw-vals))
         values                  (take-nth 2 (rest kw-vals))
         sym-vals                (apply hash-map (interleave syms  
values))
         de-map                  {:keys (vec syms)
                                  :or   sym-vals}]
     `(defn ~fn-name
        [~@pos & options#]
        (let [~de-map (apply hash-map options#)]
          ~@body))))

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.
Sean Devlin  
View profile  
 More options Jun 4 2009, 6:54 pm
From: Sean Devlin <francoisdev...@gmail.com>
Date: Thu, 4 Jun 2009 15:54:57 -0700 (PDT)
Local: Thurs, Jun 4 2009 6:54 pm
Subject: Re: defnk addition to c.c.def
Gut gemacht!

Absolutely amazing Meikel.  Now get some well earned sleep.

Sean

On Jun 4, 6:22 pm, Meikel Brandmeyer <m...@kotka.de> wrote:


 
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.
Stephen C. Gilardi  
View profile  
 More options Jun 4 2009, 7:02 pm
From: "Stephen C. Gilardi" <squee...@mac.com>
Date: Thu, 04 Jun 2009 19:02:43 -0400
Local: Thurs, Jun 4 2009 7:02 pm
Subject: Re: defnk addition to c.c.def

On Jun 4, 2009, at 6:54 PM, Sean Devlin wrote:

> Gut gemacht!

> Absolutely amazing Meikel.  Now get some well earned sleep.

> Sean

I agree. It's a really beautiful piece of code, packed full of Clojure  
goodness.

Nicely done!

I checked it into clojure.contrib.def:

        http://code.google.com/p/clojure-contrib/source/detail?r=889

Thanks!

--Steve

  smime.p7s
3K 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.
Sean Devlin  
View profile  
 More options Jun 5 2009, 8:53 am
From: Sean Devlin <francoisdev...@gmail.com>
Date: Fri, 5 Jun 2009 05:53:24 -0700 (PDT)
Local: Fri, Jun 5 2009 8:53 am
Subject: Re: defnk addition to c.c.def
I'm writing a numerical methods library, and I already found a use for
you macro.  Here's the old way:

(defn solve
  [f & params]
  (let [param-map (merge
                    {:start 1
                     :target 0
                     :diff-method :forward
                     :iter-method :newton
                     :epsilon 0.00000001
                     :max-iters 100}
                    (apply hash-map params))
        diff-method (param-map :diff-method)
        iter-method (param-map :iter-method)
        start (param-map :start)
        target (param-map :target)
        max-iters (param-map :max-iters)
        epsilon (param-map :epsilon)
        f-point #(- (f %) target)
        iter-methods {:newton (fn[x]
                                (- x (/ (f-point x) ((f-prime f-
point :diff-method diff-method) x))))}
        iterator (iter-methods iter-method)]
        (loop [x start
               iter-count 0]
          (let [next-x (iterator x)]
          (cond
            (< (java.lang.Math/abs (- (f-point x) (f-point next-x)))
epsilon) next-x
            (> iter-count max-iters) (do
                                       (println "Maximum number of
iterations reached")
                                       nil)
            true (recur next-x (inc iter-count)))))))

And here's the new way with your macro

(defnk solve-key
  [f
   :start 1
   :target 0
   :diff-method :forward
   :iter-method :newton
   :epsilon 0.00000001
   :max-iters 100]
  (let [f-point #(- (f %) target)
        iter-methods {:newton (fn[x]
                                (- x
                                   (/ (f-point x)
                                      ((f-prime f-point :diff-method diff-method) x))))}
        iterator (iter-methods iter-method)]
    (loop [x start
           iter-count 0]
      (let [next-x (iterator x)]
        (cond
         (< (java.lang.Math/abs (- (f-point x) (f-point next-x)))
            epsilon) next-x
         (> iter-count max-iters) (do
                                    (println "Maximum number of iterations reached")
                                    nil)
         true (recur next-x (inc iter-count)))))))

So it's paying off already!

On Jun 4, 7:02 pm, "Stephen C. Gilardi" <squee...@mac.com> wrote:


 
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.
Meikel Brandmeyer  
View profile  
 More options Jun 6 2009, 3:54 am
From: Meikel Brandmeyer <m...@kotka.de>
Date: Sat, 6 Jun 2009 09:54:44 +0200
Local: Sat, Jun 6 2009 3:54 am
Subject: Re: defnk addition to c.c.def

Hi,

Am 05.06.2009 um 00:22 schrieb Meikel Brandmeyer:

>        sym-vals                (apply hash-map (interleave syms  
> values))

Ah! I always forget about zipmap...

Clojure is just fun! :)

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.
End of messages
« Back to Discussions « Newer topic     Older topic »