On 29 Jul 2011, at 12:11, Ken Wesson wrote:
> 
> Why not just (vec (interleave names (take (count names) (repeat
> `(count ~foos)))))?
> 

That seems to blow up:


(defn binding-vec [foos]
  (vec (interleave names (take (count names) (repeat `(count ~foos))))))

(defmacro magic-fn
 [& forms]
 (let [args (gensym 'args)]
   `(fn [& ~args]
      (let ~(binding-vec args)
        ~@forms))))

((magic-fn (+ size 10)) 1 2) ;=> 12


> Of course I assume you eventually want something more complex than
> just (count foos) as the value for every variable, or why have more
> than one variable?

Yes, of course :-)

The actual end goal is to be able to define a macro (or whatever) passing in a 
body containing symbols which are yet to be defined. The macro will then return 
me function which has variable arity. When I call this function I'd like the 
values of the args be bound to a specified list of symbols (which happen to be 
the arg names) so that the original body can execute correctly. The interesting 
part is that I'd like to be able to specify defaults interleaved with the arg 
names. 

For example:

(def yo (with-arg-params [foo 0 bar 1 baz 2]
    (+ foo bar baz)

a would now be bound to a fn which I could call:

(yo 1 2 3) ;=> 6

I could also call it with fewer args than expected:

(yo 1 2) ;=> 5

this works because bad has a default of 2.

There's a bunch of other neat tricks I can do here which would probably only 
serve to complicate this example, but this is the general gist of things.

I assume for this to work I have to dynamically create a let binding over the 
original form, so the first call would execute code like:

(let [foo 1 bar 2 baz 3] (+ foo bar baz))

and the second example would execute code like:

(let [foo 1 bar 2 baz 2] (+ foo bar baz)

this would therefore mean that this let statement's vec needs to be a function 
of the args passed to yo and the initial set of defaults.  Unfortunately I'm 
currently unable to figure out how to achieve this.

Sam

---
http://sam.aaron.name



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