On Jul 28, 3:48 pm, Sam Aaron <samaa...@gmail.com> wrote:
> Hi there,
>
> I'm trying to create a fn which does the following:
>
> * returns a fn which takes an arbitrary number of args
> * calls a helper fn, passing the incoming args returning a vector of 
> alternating symbols and vals
> * creates a let form using the vector of alternating symbols and vals 
> returned by the helper fn as the bindings
> * contains some inner form that makes use of the bindings
>
> i.e. is it possible to implement something that allows the following to work:
>
> (defn binding-vec
>   [args]
>   ['size (count args)])
>
> (defn mk-dynamically-bound-fn
>   [form]
>   ;; returns a fn with sign [& args] which
>   ;; uses binding-vec to create a the vec for
>   ;; which a let statement binds around form
>   )
>
> (def a (mk-dynamically-bound-fn '(+ size 10)))
>
> (a 1 2 3 4) ;=> 14 (the number of args + 10)
>
> Please let me know if I'm being a bit crazy with the question. It's totally 
> possible that I'm barking up the wrong tree with this line of enquiry.

It's not clear how much of this is relevant to your actual problem, vs
the simple version you're posting here. If all you want to do is
automatically have the args counted for you, you could:

(defn wrap-counting [f]
  (fn [& args]
    (apply f (count args) args)))

(def a (wrap-counting
        (fn [size & args]
          (+ size 10))))

If you want something more general, that implicitly binds things for
you based on some code somewhere else (ew ew ew), then you need a
macro. This implementation is pretty gross; I suspect it could be
better, but you're trying to do something that seems weird to me.

(defn binding-vec []
  ['size '(count args)])

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


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

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