Skip to content

Commit cca0c8c

Browse files
committed
make this work with lein repl
1 parent 9d6b4cb commit cca0c8c

1 file changed

Lines changed: 61 additions & 23 deletions

File tree

rxjava/expt1/src/expt1/core.clj

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
;;; Load this file into LightTable (using LightTable's workspace tab)
2-
;;; and "make current editor an instarepl" (using LightTable's command
3-
;;; tab).
1+
;;; Run this file by going to the project directory (the directory
2+
;;; with 'project.clj' in it) and saying 'lein repl'.
43

54
(ns expt1.core
65
(:require [expt1.k2 :as k2 ]
@@ -28,7 +27,7 @@
2827
;;; / __|___ _ _ ___ _ _(_)__ / _ \| |__ ___ ___ _ ___ _____ _ _
2928
;;; | (_ / -_) ' \/ -_) '_| / _| | (_) | '_ (_-</ -_) '_\ V / -_) '_|
3029
;;; \___\___|_||_\___|_| |_\__| \___/|_.__/__/\___|_| \_/\___|_|
31-
30+
;;;
3231

3332
;;; The current rx library has no co-monadic operators such as "first"
3433
;;; and "last". Let us make atomic, external collectors for extracting
@@ -101,6 +100,7 @@
101100
(Observable/toObservable [1 2 3])
102101
(.take 2)
103102
(subscribe-collectors)
103+
(pdump)
104104
)
105105

106106
;;; ___ _
@@ -124,6 +124,7 @@
124124
(.mapMany
125125
#(Observable/toObservable (map (partial + %) [42 43 44])))
126126
(subscribe-collectors)
127+
(pdump)
127128
)
128129

129130
;;; Let's operate on strings.
@@ -132,6 +133,7 @@
132133
(Observable/toObservable ["one" "two" "three"])
133134
(.take 2)
134135
(subscribe-collectors)
136+
(pdump)
135137
)
136138

137139
;;; "seq" explodes strings into lazy sequences of characters:
@@ -146,6 +148,7 @@
146148
(Observable/toObservable ["one" "two" "three"])
147149
(.mapMany #(Observable/toObservable (string-explode %)))
148150
(subscribe-collectors)
151+
(pdump)
149152
)
150153

151154
;;; __
@@ -167,6 +170,7 @@
167170
(from-seq ["one" "two" "three"])
168171
(.mapMany (comp from-seq string-explode))
169172
(subscribe-collectors)
173+
(pdump)
170174
)
171175

172176
;;; _
@@ -187,6 +191,7 @@
187191
(.mapMany (comp from-seq string-explode))
188192
(.mapMany return)
189193
(subscribe-collectors)
194+
(pdump)
190195
)
191196

192197
;;; _ _ _ _ _
@@ -213,6 +218,7 @@
213218
;; work on a non-finite oseq.
214219

215220
(subscribe-collectors)
221+
(pdump)
216222
)
217223

218224
;;; Package and test.
@@ -227,6 +233,7 @@
227233
(.mapMany (comp from-seq string-explode))
228234
(distinct)
229235
(subscribe-collectors)
236+
(pdump)
230237
)
231238

232239
;;; _ _ _ _ _
@@ -260,6 +267,7 @@
260267
(.mapMany from-seq)
261268

262269
(subscribe-collectors)
270+
(pdump)
263271
)
264272

265273
;;; Better is to keep a mutable buffer of length one. It could be an atom
@@ -287,7 +295,8 @@
287295
(do
288296
(ref-set last-container [x])
289297
(return x)))))))
290-
(subscribe-collectors)))
298+
(subscribe-collectors)
299+
(pdump)))
291300

292301
;;; Package and test:
293302

@@ -308,6 +317,7 @@
308317
(.mapMany (comp from-seq string-explode))
309318
(distinct-until-changed)
310319
(subscribe-collectors)
320+
(pdump)
311321
)
312322

313323
;;; It's well-behaved on an empty input:
@@ -317,16 +327,22 @@
317327
(.mapMany (comp from-seq string-explode))
318328
(distinct-until-changed)
319329
(subscribe-collectors)
330+
(pdump)
320331
)
321332

322-
;;; ___ _ _ ___ _
323-
;;; / _ \| |_| |_ ___ _ _ | __|_ ____ _ _ __ _ __| |___ ___
324-
;;; | (_) | _| ' \/ -_) '_| | _|\ \ / _` | ' \| '_ \ / -_|_-<
325-
;;; \___/ \__|_||_\___|_| |___/_\_\__,_|_|_|_| .__/_\___/__/
326-
;;; |_|
327-
328333
(defn flip [f2] (fn [x y] (f2 y x)))
329334

335+
;;; ___ _
336+
;;; / __|_ _ _ _ __| |_ _ _ ___ _ _ ___ _ _ ___
337+
;;; \__ \ || | ' \/ _| ' \| '_/ _ \ ' \/ _ \ || (_-<
338+
;;; |___/\_, |_||_\__|_||_|_| \___/_||_\___/\_,_/__/
339+
;;; |__/
340+
;;; ___ _ _ _
341+
;;; / _ \| |__ ___ ___ _ ___ ____ _| |__| |___
342+
;;; | (_) | '_ (_-</ -_) '_\ V / _` | '_ \ / -_)
343+
;;; \___/|_.__/__/\___|_| \_/\__,_|_.__/_\___|
344+
;;;
345+
330346
(defn customObservableBlocking []
331347
"A custom Observable whose 'subscribe' method does not return until
332348
the observable completes, that is, a 'blocking' observable.
@@ -350,7 +366,20 @@
350366
(.map (partial (flip clojure.string/split) #"_"))
351367
(.map (fn [[a b]] [a (Integer/parseInt b)]))
352368
(.filter (fn [[a b]] (= 0 (mod b 7))))
353-
(subscribe-collectors))
369+
(subscribe-collectors)
370+
(pdump)
371+
)
372+
373+
;;; _ _
374+
;;; /_\ ____ _ _ _ __| |_ _ _ ___ _ _ ___ _ _ ___
375+
;;; / _ \ (_-< || | ' \/ _| ' \| '_/ _ \ ' \/ _ \ || (_-<
376+
;;; /_/ \_\/__/\_, |_||_\__|_||_|_| \___/_||_\___/\_,_/__/
377+
;;; |__/
378+
;;; ___ _ _ _
379+
;;; / _ \| |__ ___ ___ _ ___ ____ _| |__| |___
380+
;;; | (_) | '_ (_-</ -_) '_\ V / _` | '_ \ / -_)
381+
;;; \___/|_.__/__/\___|_| \_/\__,_|_.__/_\___|
382+
;;;
354383

355384
(defn customObservableNonBlocking []
356385
"A custom Observable whose 'subscribe' method returns immediately
@@ -372,7 +401,15 @@
372401
(.map (partial (flip clojure.string/split) #"_"))
373402
(.map (fn [[a b]] [a (Integer/parseInt b)]))
374403
(.filter (fn [[a b]] (= 0 (mod b 7))))
375-
(subscribe-collectors))
404+
(subscribe-collectors)
405+
(pdump)
406+
)
407+
408+
;;; _ _ __ __ _ ___
409+
;;; /_\ ____ _ _ _ __| |_ \ \ / /__| |__ | _ \__ _ __ _ ___ ___
410+
;;; / _ \ (_-< || | ' \/ _| ' \ \ \/\/ / -_) '_ \ | _/ _` / _` / -_|_-<
411+
;;; /_/ \_\/__/\_, |_||_\__|_||_| \_/\_/\___|_.__/ |_| \__,_\__, \___/__/
412+
;;; |__/ |___/
376413

377414
(defn asynchronousWikipediaArticleObservable [names]
378415
"Fetch a list of Wikipedia articles asynchronously.
@@ -383,10 +420,12 @@
383420
(let [f (future
384421
(doseq [name names]
385422
(-> observer
423+
;; Use "enlive" to parse & scrape html:
386424
(.onNext
387425
(html/html-resource
388426
(java.net.URL.
389427
(str "http://en.wikipedia.org/wiki/" name))))
428+
;; Netflix originally used strings, but...
390429
#_(.onNext (http/get
391430
(str "http://en.wikipedia.org/wiki/" name)))
392431
)
@@ -398,21 +437,20 @@
398437
(Subscriptions/create #(future-cancel f))))))
399438

400439
;;; There is something in the "Atom" web page that xml/parse does not
401-
;;; like. Rather than debug that, let's find another way to scrape the
402-
;;; web page.
440+
;;; like. Rather than debug that, let's use enlive
403441

404-
#_(defn zip-str [s]
442+
(defn zip-str [s]
405443
(zip/xml-zip
406444
(xml/parse
407445
(java.io.ByteArrayInputStream.
408446
(.getBytes s)))))
409447

410-
(pdump (->>
411-
((subscribe-collectors
412-
(asynchronousWikipediaArticleObservable ["Atom" "Molecule"])
413-
5000)
414-
:onNext)
415-
(map #(html/select % [:title]))
416-
))
448+
(->>
449+
((subscribe-collectors
450+
(asynchronousWikipediaArticleObservable ["Atom" "Molecule"])
451+
5000)
452+
:onNext)
453+
(map #(html/select % [:title]))
454+
(pdump))
417455

418456
(pdump (+ 4 3))

0 commit comments

Comments
 (0)