That was easy!

user=> (defmacro metafn [meta args & body]
   `(proxy [clojure.lang.AFn] [~meta] (invoke ~args ~...@body)))
#'user/metafn

user=> (metafn {:foo :bar} [a b c] (println a) (+ a b c))
#<AFn clojure.proxy.clojure.lang....@77309516>

user=> (*1 1 2 3)
1
6

user=> (meta (metafn {:foo :bar} [a b c] (println a) (+ a b c)))
{:foo :bar}


Unfortunately, proxying does add some overhead (about 2-10 times for  
this simple function):

user=> (let [x (metafn {:foo :bar} [a b c] (+ a b c))
              y (fn [a b c] (+ a b c))]
          (time (dotimes [i 10000] (x 1 2 3)))
          (time (dotimes [i 10000] (y 1 2 3))))
"Elapsed time: 18.492 msecs"
"Elapsed time: 8.132 msecs"

Should be fine for now; if it gets too slow I'll investigate other  
ways to build an AFn from source. Thanks for the advice!

-R

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
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