forked from omefire/ClojureProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl-utils.clj
More file actions
105 lines (80 loc) · 3.25 KB
/
repl-utils.clj
File metadata and controls
105 lines (80 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;;; TODO -- this is unfinished -- was turning into a rat-hole on Sat
;;; 23 Mar at 9:30 PM. Has not been updated since the old
;;; clojure-contrib days.
;;; Scraped from
;;; http://www.learningclojure.com/
;;;
;;; LOAD ME WITH
;;; (load-file "/Users/rebcabin/Documents/ClojureProjects/repl-utils.clj")
;;; This file conditions a repl in various ways that I do all the time.
;; Sometimes I like to ask which public functions a namespace provides.
(defn- ns-publics-list [ns] (#(list (ns-name %) (map first (ns-publics %))) ns))
;; And occasionally which functions it pulls in (with refer or use)
(defn- ns-refers-list [ns] (#(list (ns-name %) (map first (ns-refers %))) ns))
;;; It drives me up the wall that it's (doc re-pattern) but (find-doc
;;; "re-pattern"). Can use macros so that (fd re-pattern) (fd
;;; "re-pattern") and (fd 're-pattern) all mean the same thing
(defn- stringify [x]
;; (println "stringify given" (str x))
(let [s (cond (string? x) x
(symbol? x) (str x)
(and (list? x) (= (first x) 'quote)) (str (second x))
:else (str x)) ]
;; (println (str "translating to: \"" s "\""))
s))
;; Nice pretty-printed versions of these functions, accepting strings, symbols or quoted symbol
(defmacro list-publics
([]
`(clojure.pprint/pprint (ns-publics-list *ns*)))
([symbol-or-string]
`(clojure.pprint/pprint
(ns-publics-list (find-ns (symbol (stringify '~symbol-or-string)))))))
(defmacro list-refers
([]
`(clojure.pprint/pprint (ns-refers-list *ns*)))
([symbol-or-string]
`(clojure.pprint/pprint
(ns-refers-list (find-ns (symbol (stringify '~symbol-or-string)))))))
;;; List all the namespaces
(defn list-all-ns [] (clojure.pprint/pprint (map ns-name (all-ns))))
;;; List all public functions in all namespaces!
(defn list-publics-all-ns []
(clojure.pprint/pprint
(map #(list (ns-name %) (map first (ns-publics %))) (all-ns))))
;;; Def-let, for debugging lets. try
;; (def-let [r (range 10)
;; make-map (fn [f] (zipmap r (map f r)))
;; fns (map make-map (list #(* % % %) #(* 5 % %) #(* 10 %)))
;; merge-fns (fn[f] (apply merge-with f fns))]
;; (map merge-fns (list min max)))
(defmacro def-let
"like let, but binds the expressions globally."
[bindings & more]
(let [let-expr (macroexpand `(let ~bindings))
names-values (partition 2 (second let-expr))
defs (map #(cons 'def %) names-values)]
(concat (list 'do) defs more)))
;;; debugging macro try: (* 2 (dbg (* 3 4)))
(defmacro dbg [x]
`(let [x# ~x]
(do (println '~x "~~>" x#)
x#)))
;;; and pretty-printing version
(defmacro ppdbg [x]
`(let [x# ~x]
(do (println "----------------")
(clojure.pprint/pprint '~x)
(println "~~>")
(clojure.pprint/pprint x#)
(println "----------------")
x#)))
;;; Sometimes it's nice to check the classpath
(defn- get-classpath []
(sort (map (memfn getPath)
(seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))))
(defn print-classpath []
(clojure.pprint/pprint (get-classpath)))
;;; You always need to know the current directory
(defn get-current-directory []
(. (java.io.File. ".") getCanonicalPath))
(defn pwd [] (get-current-directory))