Debian Codegraph @

tests/inputs/ch10-REPL-oriented-repl-interactions.cj

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