Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "dir" equivalent in Clojure

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).

like image 623
Ali Avatar asked Jan 09 '11 04:01

Ali


2 Answers

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.

like image 143
danlei Avatar answered Oct 08 '22 23:10

danlei


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)
like image 40
Jonas Avatar answered Oct 08 '22 23:10

Jonas