Hi all,

I was curious to know if it is at all possible to determine how many times
a function has been recursively called by inspecting the callstack.

Given this code snippet:

(defn ignored? [classname]
  (let [ignored #{"callers" "dbg" "clojure.lang" "swank" "nrepl" "eval"}]
    (some #(re-find (re-pattern %) classname) ignored)))

(defn callstack []
  (let [fns (map #(str (.getClassName %))
                 (-> (Throwable.) .fillInStackTrace .getStackTrace))]
    (vec (doall (remove ignored? fns)))))

(defn random-depth []
  (pprint (callstack))
  (if (> (Math/random) 0.5)
    (recur)))

I get the following output:

["user$callstack"
 "user$random_depth"
 "clojure.main$repl$fn__7108"
 "clojure.main$repl"
 "clojure.core$apply"
 "clojure.core$with_bindings_STAR_"
 "java.util.concurrent.ThreadPoolExecutor"
 "java.util.concurrent.ThreadPoolExecutor$Worker"
 "java.lang.Thread"]
["user$callstack"
 "user$random_depth"
 "clojure.main$repl$fn__7108"
 "clojure.main$repl"
 "clojure.core$apply"
 "clojure.core$with_bindings_STAR_"
 "java.util.concurrent.ThreadPoolExecutor"
 "java.util.concurrent.ThreadPoolExecutor$Worker"
 "java.lang.Thread"]

I would like to get a different stack on each iteration, but given that the
whole point of recur is not to consume the stack while looping, I
understand why this method would not work.

Is there any other data that I could use to either get or approximate this
information?

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to