Skip to content
Newer
Older
100644 212 lines (168 sloc) 4.07 KB
7c720fe @cemerick dump of REPL interactions and other inline code snippets
cemerick authored
1 ;-----
2 (ns com.clojurebook.fn-browser
3 (:import (javax.swing JList JFrame JScrollPane JButton)
4 java.util.Vector))
5
6 (defonce fn-names (->> (ns-publics 'clojure.core)
7 (map key)
8 sort
9 Vector.
10 JList.))
11
12 (defn show-info [] )
13
14 (defonce window (doto (JFrame. "\"Interactive Development!\"")
15 (.setSize (java.awt.Dimension. 400 300))
16 (.add (JScrollPane. fn-names))
17 (.add java.awt.BorderLayout/SOUTH
18 (doto (JButton. "Show Info")
19 (.addActionListener (reify java.awt.event.ActionListener
20 (actionPerformed [_ e] (show-info))))))
21 (.setVisible true)))
22
23
24 ;-----
25 (in-ns 'com.clojurebook.fn-browser)
26
27 (import '(javax.swing JOptionPane JTextArea))
28
29 (defn show-info
30 []
31 (when-let [selected-fn (.getSelectedValue fn-names)]
32 (JOptionPane/showMessageDialog
33 window
34 (-> (ns-resolve 'clojure.core selected-fn)
35 meta
36 :doc
37 (JTextArea. 10 40)
38 JScrollPane.)
39 (str "Doc string for clojure.core/" selected-fn)
40 JOptionPane/INFORMATION_MESSAGE)))
41
42
43 ;-----
44 (split-with keyword? [:a :b :c 1 2 3])
45 ;= [(:a :b :c) (1 2 3)]
46 (zipmap (first *1) (second *1))
47 ;= {:c 3, :b 2, :a 1}
48 (apply zipmap (split-with keyword? [:a :b :c 1 2 3]))
49 ;= {:c 3, :b 2, :a 1}
50
51
52 ;-----
53 (throw (Exception. "foo"))
54 ;= Exception foo user/eval1 (NO_SOURCE_FILE:1)
55 (pst)
56 ; Exception foo
57 ; user/eval1 (NO_SOURCE_FILE:1)
58 ; clojure.lang.Compiler.eval (Compiler.java:6465)
59 ; ...
60
61
62 ;-----
63 (apropos #"^ref")
64 ;= (ref-max-history refer-clojure ref-set ref-history-count ref ref-min-history refer)
65
66
67 ;-----
68 (source merge)
69 ; (defn merge
70 ; "Returns a map that consists of the rest of the maps conj-ed onto
71 ; the first. If a key occurs in more than one map, the mapping from
72 ; the latter (left-to-right) will be the mapping in the result."
73 ; {:added "1.0"
74 ; :static true}
75 ; [& maps]
76 ; (when (some identity maps)
77 ; (reduce1 #(conj (or %1 {}) %2) maps)))
78
79
80 ;-----
81 (require 'clojure.string)
82 ;= nil
83 (dir clojure.string)
84 ; blank?
85 ; capitalize
86 ; escape
87 ; join
88 ; lower-case
89 ; replace
90 ; replace-first
91 ; reverse
92 ; split
93 ; split-lines
94 ; trim
95 ; trim-newline
96 ; triml
97 ; trimr
98 ; upper-case
99
100
101 ;-----
102 (ns clean-namespace)
103 ;= nil
104 (ns-aliases *ns*)
105 ;= {}
106 (require '[clojure.set :as set])
107 ;= nil
108 (ns-aliases *ns*)
109 ;= {set #<Namespace clojure.set>}
110 (ns-publics *ns*)
111 ;= {}
112 (def x 0)
113 ;= #'clean-namespace/x
114 (ns-publics *ns*)
115 ;= {x #'clean-namespace/x}
116
117
118 ;-----
119 (ns-unalias *ns* 'set)
120 ;= nil
121 (ns-aliases *ns*)
122 ;= {}
123 (ns-unmap *ns* 'x)
124 ;= nil
125 (ns-publics *ns*)
126 ;= {}
127
128
129 ;-----
130 (in-ns 'user)
131 ;= #<Namespace user>
132 (filter #(= 'clean-namespace (ns-name %)) (all-ns))
133 ;= (#<Namespace clean-namespace>)
134 (remove-ns 'clean-namespace)
135 ;= #<Namespace clean-namespace>
136 (filter #(= 'clean-namespace (ns-name %)) (all-ns))
137 ;= ()
138
139
140 ;-----
141 (setq inferior-lisp-program "lein repl")
142
143
144 ;-----
145 lein plugin install swank-clojure 1.3.4
146
147
148 ;-----
149 [swank-clojure "1.3.4"]
150
151
152 ;-----
153 (defn debug-me
154 [x y]
155 (let [z (merge x y)]
156 (swank.core/break)))
157
158
159 ;-----
160 (let [log-capacity 5000
161 events (agent [])]
162 (defn log-event [e]
163 (send events #(if (== log-capacity (count %))
164 (-> % (conj e) (subvec 1))
165 (conj % e)))
166 e)
167 (defn events [] @events))
168
169
170 ;-----
171 (doseq [request (repeatedly 10000 (partial rand-nth [{:referrer "twitter.com"}
172 {:referrer "facebook.com"}
173 {:referrer "twitter.com"}
174 {:referrer "reddit.com"}]))]
175 (log-event request))
176 ;= nil
177 (count (events))
178 ;= 5000
179
180
181 ;-----
182 (frequencies (events))
183 ;= {{:referrer "twitter.com"} 2502,
184 {:referrer "facebook.com"} 1280,
185 {:referrer "reddit.com"} 1218}
186
187
188 ;-----
189 (defn a [b] (+ 5 b))
190 ;= #'user/a
191 (def b (partial a 5))
192 ;= #'user/b
193 (b)
194 ;= 10
195 (defn a [b] (+ 10 b))
196 ;= #'user/a
197 (b)
198 ;= 10
199
200
201 ;-----
202 (def b (partial #'a 5))
203 ;= #'user/b
204 (b)
205 ;= 15
206 (defn a [b] (+ 5 b))
207 ;= #'user/a
208 (b)
209 ;= 10
210
211
Something went wrong with that request. Please try again.