16

Does anybody know if there is a Clojure equivalent for Pythons "dir". Basically I need to know the functions I can call on something or more specifically for java objects I want to know the methods and properties available (I am not sure if in java they are called methods and properties, this is C# lingo).

Ali
  • 18,665
  • 21
  • 103
  • 138

3 Answers3

15

clojure.contrib.repl-utils/show for use at the REPL:

user=> (use '[clojure.contrib.repl-utils :only (show)])
nil
user=> (show String)
===  public final java.lang.String  ===
[ 0] static CASE_INSENSITIVE_ORDER : Comparator
[ 1] static copyValueOf : String (char[])
[ 2] static copyValueOf : String (char[],int,int)
[ 3] static format : String (Locale,String,Object[])
[ 4] static format : String (String,Object[])
...

Alternatively, maybe something like:

user=> (map #(.getName %) (.getMethods String))
("equals" "toString" "hashCode" "compareTo" ...)

.getFields, and .getConstructors accordingly.

danlei
  • 14,121
  • 5
  • 58
  • 82
9

The clojure.repl namespace (which is available since Clojure 1.2) contains the macro dir and the function dir-fn:

user=> (clojure.repl/dir clojure.main)   
load-script
main
repl
...

user=> (clojure.repl/dir-fn 'clojure.main)
(load-script main repl repl-caught repl-exception 
 repl-prompt repl-read skip-if-eol skip-whitespace 
 with-bindings)
Jonas
  • 19,422
  • 10
  • 54
  • 67
  • Oh, I missed the introduction of `clojure.repl` (not that much time for Clojure atm) – good to know. But how do you use it with Java stuff, as asked by the OP? `(clojure.repl/dir String)` doesn't work for me. – danlei Jan 09 '11 at 08:15
  • True, it's just for Clojure namespaces. – Jonas Jan 09 '11 at 08:23
  • I wonder why they didn't include something like `show` in clojure.repl then. Anyway, +1 for your mentioning it. – danlei Jan 09 '11 at 08:27
  • 1
    Quite handy, but not I was looking for, these work on namespaces. Thanks for mentioning them though. – Ali Jan 09 '11 at 12:40
0

Since clojure.contrib is deprecated and show is not existent any more, I use this:

(use 'clojure.reflect)
(use 'clojure.pprint)

(defn py-dir [obj]
  (let [types {clojure.reflect.Field "field" clojure.reflect.Constructor "constructor" clojure.reflect.Method "method"}]
    (->> (clojure.reflect/reflect obj)
         :members ;; list of maps
         (map (fn [m] {:name (:name m) :parameter (:parameter-types m) :type (get types (type m))}))
         (sort-by (juxt :type :name))
         (clojure.pprint/print-table))))

Try:

(py-dir Integer)

It returns:

|                  :name |                           :parameter |       :type |
|------------------------+--------------------------------------+-------------|
|      java.lang.Integer |                   [java.lang.String] | constructor |
|      java.lang.Integer |                                [int] | constructor |
|                  BYTES |                                      |       field |
|              DigitOnes |                                      |       field |
|              DigitTens |                                      |       field |
|              MAX_VALUE |                                      |       field |
|              MIN_VALUE |                                      |       field |
|                   SIZE |                                      |       field |
|                   TYPE |                                      |       field |
|                 digits |                                      |       field |
|       serialVersionUID |                                      |       field |
|              sizeTable |                                      |       field |
|                  value |                                      |       field |
|               bitCount |                                [int] |      method |
|              byteValue |                                   [] |      method |
|                compare |                            [int int] |      method |
|              compareTo |                  [java.lang.Integer] |      method |
|              compareTo |                   [java.lang.Object] |      method |
|        compareUnsigned |                            [int int] |      method |
|                 decode |                   [java.lang.String] |      method |
|         divideUnsigned |                            [int int] |      method |
|            doubleValue |                                   [] |      method |
|                 equals |                   [java.lang.Object] |      method |
|             floatValue |                                   [] |      method |
|      formatUnsignedInt |             [int int byte<> int int] |      method |
|      formatUnsignedInt |             [int int char<> int int] |      method |
| formatUnsignedIntUTF16 |             [int int byte<> int int] |      method |
|               getChars |                     [int int byte<>] |      method |
|             getInteger | [java.lang.String java.lang.Integer] |      method |
|             getInteger |               [java.lang.String int] |      method |
|             getInteger |                   [java.lang.String] |      method |
|               hashCode |                                [int] |      method |
|               hashCode |                                   [] |      method |
|          highestOneBit |                                [int] |      method |
|               intValue |                                   [] |      method |
|              longValue |                                   [] |      method |
|           lowestOneBit |                                [int] |      method |
|                    max |                            [int int] |      method |
|                    min |                            [int int] |      method |
|   numberOfLeadingZeros |                                [int] |      method |
|  numberOfTrailingZeros |                                [int] |      method |
|               parseInt | [java.lang.CharSequence int int int] |      method |
|               parseInt |               [java.lang.String int] |      method |
|               parseInt |                   [java.lang.String] |      method |
|       parseUnsignedInt | [java.lang.CharSequence int int int] |      method |
|       parseUnsignedInt |               [java.lang.String int] |      method |
|       parseUnsignedInt |                   [java.lang.String] |      method |
|      remainderUnsigned |                            [int int] |      method |
|                reverse |                                [int] |      method |
|           reverseBytes |                                [int] |      method |
|             rotateLeft |                            [int int] |      method |
|            rotateRight |                            [int int] |      method |
|             shortValue |                                   [] |      method |
|                 signum |                                [int] |      method |
|             stringSize |                                [int] |      method |
|                    sum |                            [int int] |      method |
|         toBinaryString |                                [int] |      method |
|            toHexString |                                [int] |      method |
|          toOctalString |                                [int] |      method |
|               toString |                                [int] |      method |
|               toString |                            [int int] |      method |
|               toString |                                   [] |      method |
|          toStringUTF16 |                            [int int] |      method |
|         toUnsignedLong |                                [int] |      method |
|       toUnsignedString |                                [int] |      method |
|       toUnsignedString |                            [int int] |      method |
|      toUnsignedString0 |                            [int int] |      method |
|                valueOf |               [java.lang.String int] |      method |
|                valueOf |                                [int] |      method |
|                valueOf |                   [java.lang.String] |      method |

Gwang-Jin Kim
  • 9,303
  • 17
  • 30