Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Introspection in Clojure

What is the best way to do introspection in Clojure? Is there something like Python's dir function? I'm particularly interested in finding the methods that are available on java classes that I'm interoperating with, but I'm also interested in finding out anything that's available in Clojure related to introspection.

like image 345
Frank Henard Avatar asked Dec 08 '11 18:12

Frank Henard


6 Answers

Michiel Borkent and Dave Ray has the interop options covered.

For discovering Clojure functions, there are a couple of options in the clojure.repl namespace (which are likely already referred into your REPL by default).

dir:

=> (require 'clojure.set)
nil
=> (dir clojure.set)
difference
index
intersection
join
map-invert
project
rename
rename-keys
select
subset?
superset?
union

apropos:

=> (apropos #"^re-")
(re-pattern re-matches re-matcher re-groups re-find re-seq)

find-doc:

=> (find-doc #"^re-")
-------------------------
clojure.core/re-find
([m] [re s])
  Returns the next regex match, if any, of string to pattern, using
  java.util.regex.Matcher.find().  Uses re-groups to return the
  groups.
-------------------------
clojure.core/re-groups
([m])
  Returns the groups from the most recent match/find. If there are no
  nested groups, returns a string of the entire match. If there are
  nested groups, returns a vector of the groups, the first element
  being the entire match.
-------------------------
....
like image 85
cemerick Avatar answered Nov 01 '22 17:11

cemerick


If you want to discover methods, just use plain Java reflection:

user=> (.getDeclaredMethods (.getClass {:a 1}))
#<Method[] [Ljava.lang.reflect.Method;@72b398da>
user=> (pprint *1)
[#<Method private int clojure.lang.PersistentArrayMap.indexOf(java.lang.Object)>,
 #<Method public int clojure.lang.PersistentArrayMap.count()>,
 #<Method public java.util.Iterator clojure.lang.PersistentArrayMap.iterator()>,
 #<Method public boolean clojure.lang.PersistentArrayMap.containsKey(java.lang.Object)>,
 #<Method public int clojure.lang.PersistentArrayMap.capacity()>,
 #<Method public clojure.lang.IPersistentMap clojure.lang.PersistentArrayMap.empty()>,
 ...

You can also write it a little nicer with a threading macro:

(-> {:a 1} .getClass .getDeclaredMethods pprint)

or

(-> clojure.lang.PersistentArrayMap .getDeclaredMethods pprint)

(I just learned from #clojure IRC that the name of the class itself is already the Class object!)

like image 45
Michiel Borkent Avatar answered Nov 01 '22 17:11

Michiel Borkent


If you're using 1.3, there's clojure.reflect which allows you to inspect, manipulate, etc. info about Java classes. I'm also fond of the clojure.contrib.repl-utils/show from old contrib if that's an option for you.

like image 27
Dave Ray Avatar answered Nov 01 '22 19:11

Dave Ray


Check out clojure.contrib.repl-utils, and show in particular.

http://richhickey.github.com/clojure-contrib/repl-utils-api.html#clojure.contrib.repl-utils/show

These are utilities for you to introspect objects and classes in the REPL, but the code is a good example of how to explore classes programatically.

like image 44
John Cromartie Avatar answered Nov 01 '22 19:11

John Cromartie


To find interfaces implemented by a class, try supers

(supers clojure.lang.PersistentHashMap)

like image 24
Malcolm Sparks Avatar answered Nov 01 '22 17:11

Malcolm Sparks


clojure.contrib.repl-utils has moved to clojure.reflect.

You can now call it as the following:

(->> (clojure.reflect/reflect java.lang.String) 
     :members 
     clojure.pprint/pprint)

https://stackoverflow.com/a/17974908/262425

like image 26
ruseel Avatar answered Nov 01 '22 17:11

ruseel