Python “dir” equivalent in Clojure

走远了吗. 提交于 2019-12-03 11:37:50

问题


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


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/4637615/python-dir-equivalent-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!