-
Notifications
You must be signed in to change notification settings - Fork 88
/
ch12-patterns-repl-interactions.clj
392 lines (297 loc) · 7.18 KB
/
ch12-patterns-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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
;-----
(defn- update-status*
[service-name service-endpoint-url request-data-fn]
(fn [new-status]
(log (format "Updating status @ %s to %s" service-name new-status))
(let [http-request-data (request-data-fn new-status)
connection (-> service-endpoint-url java.net.URL. .openConnection)]
;; ...set request method, parameters, body on `connection`
;; ...perform actual request
;; ...return result based on HTTP response status
)))
(def update-facebook-status (update-status* "Facebook" "http://facebook.com/apis/..."
(fn [status]
{:params {:_a "update_status"
:_t status}
:method "GET"})))
(def update-twitter-status ...)
(def update-google-status ...)
;-----
interface IDog {
public String bark();
}
class Chihuahua implements IDog {
public String bark() {
return "Yip!";
}
}
class Mastiff implements IDog {
public String bark() {
return "Woof!";
}
}
class PetStore {
private IDog dog;
public PetStore() {
this.dog = new Mastiff();
}
public IDog getDog() {
return dog;
}
}
static class MyApp {
public static void main(String[] args) {
PetStore store = new PetStore();
System.out.println(store.getDog().bark());
}
}
;-----
class PetStore {
private IDog dog;
public PetStore(IDog dog) {
this.dog = dog;
}
public IDog getDog() {
return dog;
}
}
class MyApp {
public static void main(String[] args) {
PetStore store = new PetStore(new Chihuahua());
System.out.println(store.getDog().bark());
}
}
;-----
(defprotocol Bark
(bark [this]))
(defrecord Chihuahua []
Bark
(bark [this] "Yip!"))
(defrecord Mastiff []
Bark
(bark [this] "Woof!"))
;-----
(defrecord PetStore [dog])
(defn main
[dog]
(let [store (PetStore. dog)]
(println (bark (:dog store)))))
(main (Chihuahua.))
;= Yip!
(main (Mastiff.))
;= Woof!
;-----
(extend-protocol Bark
java.util.Map
(bark [this]
(or (:bark this)
(get this "bark"))))
;-----
(main (doto (java.util.HashMap.)
(.put "bark" "Ouah!")))
;= Ouah!
(main {:bark "Wan-wan!"})
;= Wan wan!
;-----
{:dog #user.Chihuahua{:weight 12, :price "$84.50"}}
;-----
(defn configured-petstore
[]
(-> "petstore-config.clj"
slurp
read-string
map->PetStore))
;-----
(configured-petstore)
;= #user.PetStore{:dog #user.Chihuahua{:weight 12, :price "$84.50"}}
;-----
interface ISorter {
public sort (int[] numbers);
}
class QuickSort implements ISorter {
public sort (int[] numbers) { ... }
}
class MergeSort implements ISorter {
public sort (int[] numbers) { ... }
}
class Sorter {
private ISorter sorter;
public Sorter (ISorter sorter) {
this.sorter = sorter;
}
public execute (int[] numbers) {
sorter.sort(numbers);
}
}
class App {
public ISorter chooseSorter () {
if (...) {
return new QuickSort();
} else {
return new MergeSort();
}
}
public static void main(String[] args) {
int[] numbers = {5,1,4,2,3};
Sorter s = new Sorter(chooseSorter());
s.execute(numbers);
//... now use sorted numbers
}
}
;-----
(defn quicksort [numbers] ...)
(defn mergesort [numbers] ...)
(defn choose-sorter
[]
(if ...
quicksort
mergesort))
(defn main
[]
(let [numbers [...]]
((choose-sorter) numbers)))
;-----
((comp reverse sort) [2 1 3])
;= (3 2 1)
;-----
abstract class Processor {
protected Processor next;
public addToChain(Processor p) {
next = p;
}
public runChain(data) {
Boolean continue = this.process(data);
if(continue and next != null) {
next.runChain(data);
}
}
abstract public boolean process(String data);
}
class FooProcessor extends Processor {
public boolean process(String data) {
System.out.println("FOO says pass...");
return true;
}
}
class BarProcessor extends Processor {
public boolean process(String data) {
System.out.println("BAR " + data + " and let's stop here");
return false;
}
}
class BazProcessor extends Processor {
public boolean process(String data) {
System.out.println("BAZ?");
return true;
}
}
Processor chain = new FooProcessor().addToChain(new BarProcessor).addToChain(new BazProcessor);
chain.run("data123");
;-----
(defn foo [data]
(println "FOO passes")
true)
(defn bar [data]
(println "BAR" data "and let's stop here")
false)
(defn baz [data]
(println "BAZ?")
true)
(defn wrap [f1 f2]
(fn [data]
(when (f1 data)
(f2 data))))
(def chain (reduce wrap [foo bar baz]))
;-----
(defn my-app
[request]
{:status 200
:headers {"Content-type" "text/html"}
:body (format "<html><body>You requested: %s</body></html>"
(:uri request))})
;-----
(defn wrap-logger
[handler]
(fn [request]
(println (:uri request))
(handler request)))
;-----
(require '[ring.middleware cookies session])
(def my-app (-> my-app
wrap-cookies
wrap-session
wrap-logger))
;-----
public class Foo
public void expensiveComputation () {
long start = System.currentTimeMillis();
try {
// do computation
} catch (Exception e) {
// log error
} finally {
long stop = System.currentTimeMillis();
System.out.println("Run time: " + (stop - start) + "ms");
}
}
}
;-----
public class AspectJExample {
public void longRunningMethod () {
System.out.println("Starting long-running method");
try {
Thread.sleep((long)(1000 + Math.random() * 2000));
} catch (InterruptedException e) {
}
}
}
;-----
public aspect Timing {
pointcut profiledMethods(): call(* AspectJExample.* (..));
long time;
before(): profiledMethods() {
time = System.currentTimeMillis();
}
after(): profiledMethods() {
System.out.println("Call to " + thisJoinPoint.getSignature() +
" took " + (System.currentTimeMillis() - time) + "ms");
}
}
;-----
Starting long-running method
Call to void com.clojurebook.AspectJExample.longRunningMethod() took 1599ms
;-----
(defn time-it [f & args]
(let [start (System/currentTimeMillis)]
(try
(apply f args)
(finally
(println "Run time: " (- (System/currentTimeMillis) start) "ms")))))
;-----
(require 'robert.hooke)
(defn foo [x y]
(Thread/sleep (rand-int 1000))
(+ x y))
(robert.hooke/add-hook #'foo time-it)
;-----
(foo 1 2)
; Run time: 772 ms
;= 3
;-----
(robert.hooke/with-hooks-disabled foo (foo 1 2))
;= 3
(robert.hooke/remove-hook #'foo time-it)
;= #<user$foo user$foo@4f13f501>
(foo 1 2)
;= 3
;-----
(require 'clojure.set)
;= nil
(doseq [var (->> (ns-publics 'clojure.set)
(map val))]
(robert.hooke/add-hook var time-it))
;= nil
(clojure.set/intersection (set (range 100000))
(set (range -100000 10)))
; Run time: 97 ms
;= #{0 1 2 3 4 5 6 7 8 9}