Re: Cheap way to find function (defn) name using a macro?

2012-03-30 Thread Cedric Greevey
On Thu, Mar 29, 2012 at 11:59 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: The change needs to be least intrusive and doesn't justify exposing more surface area than it should. It's a trade off. Injecting a version of defn that doesn't do anything different except make a new thing

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Sun Ning
Since you want to use the plain defn, what about thinking in a different way ? Write a macro like `with-function-name`, wraps the function call instead of the function definition. (defmacro with-function-name [fn-name args] ...) In clojure, because you can assign function to a var at any

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Cedric Greevey
On Wed, Mar 28, 2012 at 11:48 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: If you control the third line of: (defn foo [x y]   (let [z (bar y (next x))]     (println Done in (find-name) .)     (* 4 z (count x then don't you control the first? Cedric – Unfortunately, no. The

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Shantanu Kumar
On Mar 29, 5:50 pm, Cedric Greevey cgree...@gmail.com wrote: On Wed, Mar 28, 2012 at 11:48 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: If you control the third line of: (defn foo [x y]   (let [z (bar y (next x))]     (println Done in (find-name) .)     (* 4 z (count x

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Cedric Greevey
On Thu, Mar 29, 2012 at 11:35 AM, Shantanu Kumar kumar.shant...@gmail.com wrote: On Mar 29, 5:50 pm, Cedric Greevey cgree...@gmail.com wrote: On Wed, Mar 28, 2012 at 11:48 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: If you control the third line of: (defn foo [x y]   (let [z

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Shantanu Kumar
81  (defn foo [...] 82    (let [x (compute-something ...)] 83      (do-something x ...) 84      (calculate-whatever ...))) and you're able to edit lines 82, 83, and 84 but not line 81 (or whatever). But I can't see any plausible circumstance where that would be the case. That's exactly

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Cedric Greevey
On Thu, Mar 29, 2012 at 2:36 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: 81  (defn foo [...] 82    (let [x (compute-something ...)] 83      (do-something x ...) 84      (calculate-whatever ...))) and you're able to edit lines 82, 83, and 84 but not line 81 (or whatever). But I can't

Re: Cheap way to find function (defn) name using a macro?

2012-03-29 Thread Shantanu Kumar
On Mar 29, 11:46 pm, Cedric Greevey cgree...@gmail.com wrote: On Thu, Mar 29, 2012 at 2:36 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: 81  (defn foo [...] 82    (let [x (compute-something ...)] 83      (do-something x ...) 84      (calculate-whatever ...))) and

Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Shantanu Kumar
Hi, Is it possible to write a macro that when used in a top-level function (created using defn) can find out the name of the immediate top-level function? I know *ns* returns the namespace and it's possible to walk the current thread's stack trace to find out the function name, but I am looking

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Ram Krishnan
Did you just need the name of the function? something like this? 88 (def ^{:dynamic true} *myself* nil) (defmacro defn* [name args body] `(defn ~name ~args (binding [*myself* '~name] ~@body))) (defn* foo [a b] [*myself* (+ a b)])

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Shantanu Kumar
On Mar 28, 8:57 pm, Ram Krishnan kriyat...@gmail.com wrote: Did you just need the name of the function? something like this? Sorry, I should explained using code what I am looking for: (defmacro find-name [] ;; some magic here ..) (defn foo [] .. (println (find-name)) ; should print

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Rostislav Svoboda
Sorry, I should explained using code what I am looking for: Oh yea!!! (defmacro find-name []  ;; some magic here  ..) (defn foo []  ..  (println (find-name)) ; should print foo  ..) I am looking for a way to write the `find-name` macro. So, you can see I want to use it with regular

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Steve Miner
I don't know how to get the fn name at runtime, except by looking at the stacktrace which is going to expensive as you mentioned. ;; original: ;; http://groups.google.com/group/clojure/browse_thread/thread/234ac3ff0a4b6b80?pli=1 ;; but slightly changed for Clojure updates since 1.0 (defn

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Ram Krishnan
On Wednesday, March 28, 2012 at 9:15 AM, Shantanu Kumar wrote: On Mar 28, 8:57 pm, Ram Krishnan kriyat...@gmail.com (http://gmail.com) wrote: Did you just need the name of the function? something like this? Sorry, I should explained using code what I am looking for: (defmacro

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Shantanu Kumar
Thanks all for responding. Ram – Appreciate both versions, but I need to use it with regular functions created with `defn`. Rostislav – I need to do this in a regular function created using `defn`; not sure how can I use named let in this case. Am I missing something? Steve – Your example is

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Thomas Winant
On 28/03/2012 18:15, Shantanu Kumar wrote: On Mar 28, 8:57 pm, Ram Krishnan kriyat...@gmail.com wrote: Did you just need the name of the function? something like this? Sorry, I should explained using code what I am looking for: (defmacro find-name [] ;; some magic here ..) (defn foo

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Rostislav Svoboda
Steve – Your example is already compact, but maybe it can be made even cheaper by specifying maxDepth of 1: http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#getThreadInfo(long, int) @Shantanu: What about the defn0 macro from Steve: (defmacro defn0 [name fdcls]

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Cedric Greevey
On Wed, Mar 28, 2012 at 3:30 PM, Rostislav Svoboda rostislav.svob...@gmail.com wrote: Steve – Your example is already compact, but maybe it can be made even cheaper by specifying maxDepth of 1: http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#getThreadInfo(long,

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Shantanu Kumar
Rostislav and Cedric – I cannot supply my own version of defn; the macro should work with the regular clojure.core/defn. Thomas – Your example works well with Clojure 1.2.x but doesn't with Clojure 1.3 and 1.4. As you rightly said, env should not be relied upon. :) Shantanu On Mar 29, 4:17 am,

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Cedric Greevey
On Wed, Mar 28, 2012 at 10:58 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: Rostislav and Cedric – I cannot supply my own version of defn; the macro should work with the regular clojure.core/defn. If you control the third line of: (defn foo [x y] (let [z (bar y (next x))] (println

Re: Cheap way to find function (defn) name using a macro?

2012-03-28 Thread Shantanu Kumar
If you control the third line of: (defn foo [x y]   (let [z (bar y (next x))]     (println Done in (find-name) .)     (* 4 z (count x then don't you control the first? Cedric – Unfortunately, no. The target is pre-written code that may have been created using (1) clojure.core/defn or