Posted By

narkisr on 06/04/09


Tagged

show functions clojure source REPL


Versions (?)

Viewing source & type methods in Clojure REPL


 / Published in: Lisp
 

Clojure contribe repl-utils offers two functions for locating the source code of functions & the method of types (http://groups.google.com/group/clojure/browse_thread/thread/77f95518fcfc8a08/5de8ef8ff8f3b3bd?lnk=gst&q=source#5de8ef8ff8f3b3bd).

  1. user=> (use 'clojure.contrib.repl-utils)
  2.  
  3. ; shows the source of the filter method
  4. user=> (source filter)
  5. (defn filter
  6. "Returns a lazy sequence of the items in coll for which
  7. (pred item) returns true. pred must be free of side-effects."
  8. [pred coll]
  9. (let [step (fn [p c]
  10. (when-let [s (seq c)]
  11. (if (p (first s))
  12. (cons (first s) (filter p (rest s)))
  13. (recur p (rest s)))))]
  14. (lazy-seq (step pred coll))))
  15.  
  16. ; shows all the methods of Clojure ratio type
  17. user=> (show 1/2)
  18. === public clojure.lang.Ratio ===
  19. [ 0] <init> (BigInteger,BigInteger)
  20. [ 1] denominator : BigInteger
  21. [ 2] numerator : BigInteger
  22. [ 3] byteValue : byte ()
  23. [ 4] compareTo : int (Object)
  24. [ 5] decimalValue : BigDecimal ()
  25. [ 6] decimalValue : BigDecimal (MathContext)
  26. [ 7] doubleValue : double ()
  27. [ 8] equals : boolean (Object)
  28. [ 9] floatValue : float ()
  29. [10] getClass : Class ()
  30. [11] hashCode : int ()
  31. [12] intValue : int ()
  32. [13] longValue : long ()
  33. [14] notify : void ()
  34. [15] notifyAll : void ()
  35. [16] shortValue : short ()
  36. [17] toString : String ()
  37. [18] wait : void ()
  38. [19] wait : void (long)
  39. [20] wait : void (long,int)

Report this snippet  

You need to login to post a comment.