On Thu, Nov 20, 2008 at 10:03 AM, wlr <[EMAIL PROTECTED]> wrote:
>
> As stated in http://clojure.org/getting_started if user.clj is found
> on the classpath it will be autoloaded at clojure startup. Answers to
> the subject question will perhaps change as users and clojure mature
> together, but for now  will you divulge (some of) your user.clj?
>
> So far for me: user.clj is empty

I used to use user.clj for the following, but that doesn't always work
right (did it have something to do with *print-length* not being bound
for clojure.lang.Script? I'm don't quite remember...)  So anyway, I
have a little shell script that runs clojure for me with the right
classpath and, when launching a REPL, with ~/.clojurerc.clj on its
command line.  In .clojurerc.clj, I have:

(import '(java.io LineNumberReader InputStreamReader PushbackReader)
        '(java.lang.reflect Modifier Method Constructor)
        '(clojure.lang RT))

(set! *print-length* 103)

(defn show
  ([x] (show x nil))
  ([x i]
      (let [c (if (class? x) x (class x))
            items (sort
                    (for [m (concat (.getFields c)
                                    (.getMethods c)
                                    (.getConstructors c))]
                      (let [static? (bit-and Modifier/STATIC
                                             (.getModifiers m))
                            method? (instance? Method m)
                            ctor?   (instance? Constructor m)
                            text (if ctor?
                                   (str "(" (apply str (interpose ", "
(.getParameterTypes m))) ")")
                                   (str
                                     (if (pos? static?) "static ")
                                     (.getName m) " : "
                                     (if method?
                                       (str (.getReturnType m) " ("
                                            (count (.getParameterTypes m)) ")")
                                       (str (.getType m)))))]
                        [(- static?) method? text (str m) m])))]
        (if i
          (last (nth items i))
          (do (println "=== " c " ===")
            (doseq [[e i] (map list items (iterate inc 0))]
              (printf "[%2d] %s\n" i (nth e 2))))))))

*print-length* is documented elsewhere.
show works like this:

user=> (show Object) ; give it a class
===  java.lang.Object  ===
[ 0] ()
[ 1] equals : boolean (1)
[ 2] getClass : class java.lang.Class (0)
[ 3] hashCode : int (0)
[ 4] notify : void (0)
[ 5] notifyAll : void (0)
[ 6] toString : class java.lang.String (0)
[ 7] wait : void (0)
[ 8] wait : void (1)
[ 9] wait : void (2)
nil
user=> (show Object 1) ; a class and a method number to see details
#<Method public boolean java.lang.Object.equals(java.lang.Object)>
user=> (show {}) ; or give it an instance
===  clojure.lang.PersistentHashMap  ===
[ 0] static EMPTY : class clojure.lang.PersistentHashMap
[ 1] static applyToHelper : class java.lang.Object (2)
[ 2] static create : class clojure.lang.PersistentHashMap (1)
[ 3] static create : class clojure.lang.PersistentHashMap (1)
[ 4] static create : class clojure.lang.PersistentHashMap (1)
[ 5] static create : class clojure.lang.PersistentHashMap (2)
[ 6] static create : interface clojure.lang.IPersistentMap (1)
[ 7] (interface clojure.lang.IPersistentMap, int, interface
clojure.lang.PersistentHashMap$INode)
[ 8] applyTo : class java.lang.Object (1)
[ 9] assoc : interface clojure.lang.Associative (2)
...

It's like interactive javadoc, without all those distracting English
descriptions of things. :-)

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
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