Reputation: 19672
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).
Upvotes: 16
Views: 1272
Reputation: 9865
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 |
Upvotes: 0
Reputation: 14291
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.
Upvotes: 15
Reputation: 19632
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)
Upvotes: 9