daily pastebin goal
17%
SHARE
TWEET

require-all-snippet.clj

a guest Apr 14th, 2012 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; thanks to http://www.learningclojure.com/2010/03/conditioning-repl.html
  2. ;; LOAD ME WITH
  3. ;; (load-file "D:/mannai/require-all-snippet.clj")
  4. ;; This file conditions a repl in various ways that I do all the time.
  5. ;; Firstly we want to 'require' all the namespaces on the classpath
  6. ;; This ensures that find-doc and the like will work
  7.  
  8. ;; Add to the classpath project (on lein, project.clj):
  9. ;;    [org.clojure/tools.trace "0.7.3"]     -> tools.trace-0.7.3.jar
  10. ;;    [org.clojure/tools.namespace "0.1.0"] -> tools.namespace-0.1.0.jar
  11.  
  12. (use 'clojure.tools.trace)
  13. (use 'clojure.tools.namespace)
  14.  
  15. (use 'clojure.pprint)
  16. (use 'clojure.inspector)
  17.  
  18.  
  19. ;; Some namespaces may fail to load, so catch any exceptions thrown
  20. (defn- require-may-fail [ns]
  21.   (try
  22.  
  23.    (print "Attempting to require " ns ": ")
  24.    (require ns)
  25.    (println "success")
  26.   (catch Throwable e (println "couldn't require " ns "\nException\n" e "\n\n"))
  27.    ))
  28.  
  29.  
  30. ;; Generally we'd want clojure.*, clojure.contrib.*, and any project-specific namespaces
  31. (defn require-all-namespaces-starting-with [strng]
  32.   (doall (map require-may-fail
  33.               (filter #(. (str %) startsWith strng)
  34.                       (find-namespaces-on-classpath)))))
  35.  
  36.  
  37. ;; The functions in these namespaces are so useful at the REPL that I want them 'use'd.
  38.  
  39.  
  40. ;; It drives me up the wall that it's (doc re-pattern) but (find-doc "re-pattern").
  41. ;; Can use macros so that (fd re-pattern) (fd "re-pattern") and (fd 're-pattern) all mean the same thing
  42. (defn- stringify [x]
  43.   (println "stringify given" (str x))
  44.   (let [s  (cond (string? x) x
  45.                  (symbol? x) (str x)
  46.                  (and (list? x) (= (first x) 'quote)) (str (second x))
  47.                  :else (str x)) ]
  48.     (println (str "translating to: \"" s "\""))
  49.     s))
  50.  
  51.  
  52.  
  53. ;; Sometimes I like to ask which public functions a namespace provides.
  54. (defn ns-publics-list [ns] (#(list (ns-name %) (map first (ns-publics %))) ns))
  55. ;; And occasionally which functions it pulls in (with refer or use)
  56. (defn ns-refers-list  [ns] (#(list (ns-name %) (map first (ns-refers %))) ns))
  57.  
  58.  
  59. ;; Nice pretty-printed versions of these functions, accepting strings, symbols or quoted symbol
  60. (defmacro list-publics    
  61.   ([]   `(pprint (ns-publics-list *ns*)))
  62.   ([symbol-or-string] `(pprint (ns-publics-list (find-ns (symbol (stringify '~symbol-or-string)))))))
  63.  
  64. (defmacro list-refers
  65.   ([]   `(pprint (ns-refers-list *ns*)))
  66.   ([symbol-or-string] `(pprint (ns-refers-list (find-ns (symbol (stringify '~symbol-or-string)))))))
  67.  
  68. ;; List all the namespaces
  69. (defn list-all-ns [] (pprint (map ns-name (all-ns))))
  70.  
  71. ;; List all public functions in all namespaces!
  72. (defn list-publics-all-ns [] (pprint (map #(list (ns-name %) (map first (ns-publics %))) (all-ns))))
  73.  
  74. ;; With all the namespaces loaded, find-doc can be overwhelming.
  75. ;; This is like find-doc, but just gives the associated names.
  76.  
  77. (defn- find-doc-names
  78.   "Prints the name of any var whose documentation or name contains a match for re-string-or-pattern"
  79.   [re-string-or-pattern]
  80.     (let [re  (re-pattern re-string-or-pattern)]
  81.       (doseq [ns (all-ns)
  82.               v (sort-by (comp :name meta) (vals (ns-interns ns)))
  83.               :when (and (:doc (meta v))
  84.                          (or (re-find (re-matcher re (:doc (meta v))))
  85.                              (re-find (re-matcher re (str (:name (meta v)))))))]
  86.                (print v "\n"))))
  87.  
  88.  
  89.  
  90.  
  91.  
  92. ;;find symbol or string in docs
  93. (defmacro fd [symbol-or-string] `(clojure.repl/find-doc (stringify '~symbol-or-string)))
  94.  
  95. (defmacro fdn [symbol-or-string] `(find-doc-names (stringify '~symbol-or-string)))
  96.  
  97.  
  98.  
  99. ;;debugging macro                                try: (* 2 (dbg (* 3 4)))
  100. (defmacro dbg [x] `(let [x# ~x] (do (println '~x "->" x#) x#)))
  101.  
  102. ;;and pretty-printing version
  103. (defmacro ppdbg [x]`(let [x# ~x] (do (println "--")(pprint '~x)(println "->")(pprint x#) (println "--") x#)))
  104.  
  105.  
  106. ;;and one for running tests
  107. (defmacro run-test [fn] `(test (resolve '~fn)))
  108.  
  109.  
  110. ;; Sometimes it's nice to check the classpath
  111. (defn- get-classpath []
  112.    (sort (map (memfn getPath)
  113.               (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))))
  114.  
  115. (defn print-classpath []
  116.   (pprint (get-classpath)))
  117.  
  118. (defn get-current-directory []
  119.   (. (java.io.File. ".") getCanonicalPath))
  120.  
  121. ;;require everything from clojure and clojure.contrib, so that find-doc can find it
  122. (require-all-namespaces-starting-with "clojure")
  123.  
  124. ;;print the classpath
  125. (println "Classpath:")
  126. (print-classpath)
  127.  
  128. (println "Current Directory" (get-current-directory))
  129.  
  130. ;;print the public functions in the current namespace
  131. (println "Current Namespace")
  132. (list-publics)
  133.  
  134.  
  135. ;;hint on how to require project specific namespaces
  136. (println "to require all namespaces starting with example:")
  137. (println "(require-all-namespaces-starting-with \"example\")")
RAW Paste Data
Top