On 22 Okt., 23:24, André Thieme <[EMAIL PROTECTED]> wrote:

> This version of replace-placeholders still misses the numbered
> args %1, %2, .. as the reader would also steal those from me.
> In the end the #(..) macro would do it, instead of §.

So what I just did was extending it for this demostration by using
$1, $2, .. instead.

First we need two little helpers:
(defn digit-char? [char]
  ({\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9} char))

(defn numbered-arg? [arg]
  (let [[$ & nums] (str arg)]
    (and (= $ \$)
         (every? digit-char? nums))))

Btw, something like digit-char? would be nice for the boot.clj

And now caring for the $n case:
(defn replace-placeholders2 [body]
  ((fn [args num-args new-body [f & r :as b]]
    (if (not b)
        [(seq args) (seq new-body)]
        (cond (= f '_) (let [gensym (gensym)]
                         (recur (conj args gensym)
                                num-args
                                (conj new-body gensym)
                                r))
              (numbered-arg? f) (if-let val (get num-args f)
                                  (recur args num-args (conj new-body
val) r)
                                  (let [gensym (gensym)]
                                    (recur (conj args gensym)
                                           (assoc num-args f gensym)
                                           (conj new-body gensym)
                                           r)))
              :default (recur args num-args (conj new-body f) r))))
   [] {} [] body))


As I see it this might even support nested currying if only the outer
one uses $1, $2, $3, ...

Example:   #(map #(+ 3) %1 %1)

Translated §(map §(+ 3) $1 $1) and moved the § inside:

user> ((§ map (§ + 3) $1 $1) (range 1 10) (range 200 300) (range 5000
6000))
(5205 5209 5213 5217 5221 5225 5229 5233 5237)

--~--~---------~--~----~------------~-------~--~----~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to