-
Notifications
You must be signed in to change notification settings - Fork 88
/
ch10-REPL-oriented-repl-interactions.clj
211 lines (168 loc) · 4.07 KB
/
ch10-REPL-oriented-repl-interactions.clj
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;-----
(ns com.clojurebook.fn-browser
(:import (javax.swing JList JFrame JScrollPane JButton)
java.util.Vector))
(defonce fn-names (->> (ns-publics 'clojure.core)
(map key)
sort
Vector.
JList.))
(defn show-info [] )
(defonce window (doto (JFrame. "\"Interactive Development!\"")
(.setSize (java.awt.Dimension. 400 300))
(.add (JScrollPane. fn-names))
(.add java.awt.BorderLayout/SOUTH
(doto (JButton. "Show Info")
(.addActionListener (reify java.awt.event.ActionListener
(actionPerformed [_ e] (show-info))))))
(.setVisible true)))
;-----
(in-ns 'com.clojurebook.fn-browser)
(import '(javax.swing JOptionPane JTextArea))
(defn show-info
[]
(when-let [selected-fn (.getSelectedValue fn-names)]
(JOptionPane/showMessageDialog
window
(-> (ns-resolve 'clojure.core selected-fn)
meta
:doc
(JTextArea. 10 40)
JScrollPane.)
(str "Doc string for clojure.core/" selected-fn)
JOptionPane/INFORMATION_MESSAGE)))
;-----
(split-with keyword? [:a :b :c 1 2 3])
;= [(:a :b :c) (1 2 3)]
(zipmap (first *1) (second *1))
;= {:c 3, :b 2, :a 1}
(apply zipmap (split-with keyword? [:a :b :c 1 2 3]))
;= {:c 3, :b 2, :a 1}
;-----
(throw (Exception. "foo"))
;= Exception foo user/eval1 (NO_SOURCE_FILE:1)
(pst)
; Exception foo
; user/eval1 (NO_SOURCE_FILE:1)
; clojure.lang.Compiler.eval (Compiler.java:6465)
; ...
;-----
(apropos #"^ref")
;= (ref-max-history refer-clojure ref-set ref-history-count ref ref-min-history refer)
;-----
(source merge)
; (defn merge
; "Returns a map that consists of the rest of the maps conj-ed onto
; the first. If a key occurs in more than one map, the mapping from
; the latter (left-to-right) will be the mapping in the result."
; {:added "1.0"
; :static true}
; [& maps]
; (when (some identity maps)
; (reduce1 #(conj (or %1 {}) %2) maps)))
;-----
(require 'clojure.string)
;= nil
(dir clojure.string)
; blank?
; capitalize
; escape
; join
; lower-case
; replace
; replace-first
; reverse
; split
; split-lines
; trim
; trim-newline
; triml
; trimr
; upper-case
;-----
(ns clean-namespace)
;= nil
(ns-aliases *ns*)
;= {}
(require '[clojure.set :as set])
;= nil
(ns-aliases *ns*)
;= {set #<Namespace clojure.set>}
(ns-publics *ns*)
;= {}
(def x 0)
;= #'clean-namespace/x
(ns-publics *ns*)
;= {x #'clean-namespace/x}
;-----
(ns-unalias *ns* 'set)
;= nil
(ns-aliases *ns*)
;= {}
(ns-unmap *ns* 'x)
;= nil
(ns-publics *ns*)
;= {}
;-----
(in-ns 'user)
;= #<Namespace user>
(filter #(= 'clean-namespace (ns-name %)) (all-ns))
;= (#<Namespace clean-namespace>)
(remove-ns 'clean-namespace)
;= #<Namespace clean-namespace>
(filter #(= 'clean-namespace (ns-name %)) (all-ns))
;= ()
;-----
(setq inferior-lisp-program "lein repl")
;-----
lein plugin install swank-clojure 1.3.4
;-----
[swank-clojure "1.3.4"]
;-----
(defn debug-me
[x y]
(let [z (merge x y)]
(swank.core/break)))
;-----
(let [log-capacity 5000
events (agent [])]
(defn log-event [e]
(send events #(if (== log-capacity (count %))
(-> % (conj e) (subvec 1))
(conj % e)))
e)
(defn events [] @events))
;-----
(doseq [request (repeatedly 10000 (partial rand-nth [{:referrer "twitter.com"}
{:referrer "facebook.com"}
{:referrer "twitter.com"}
{:referrer "reddit.com"}]))]
(log-event request))
;= nil
(count (events))
;= 5000
;-----
(frequencies (events))
;= {{:referrer "twitter.com"} 2502,
{:referrer "facebook.com"} 1280,
{:referrer "reddit.com"} 1218}
;-----
(defn a [b] (+ 5 b))
;= #'user/a
(def b (partial a 5))
;= #'user/b
(b)
;= 10
(defn a [b] (+ 10 b))
;= #'user/a
(b)
;= 10
;-----
(def b (partial #'a 5))
;= #'user/b
(b)
;= 15
(defn a [b] (+ 5 b))
;= #'user/a
(b)
;= 10