/
coroutines.clj
304 lines (258 loc) · 8.51 KB
/
coroutines.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
(ns hoare.coroutines
(:require [clojure.core.async :refer :all]))
;; Examples corresponding to those in section 3 of C.A.R. Hoare,
;; "Communicating Sequential Processes" (CACM 21:8 August 1978).
;; This paper is the precursor to his book of the same title,
;; which can be obtained at http://www.usingcsp.com/
(defmacro go-times [bindings & body]
`(go (dotimes ~bindings
~@body)))
;; 3.1 COPY
;; The first common pattern of the Go language is
;; GENERATOR: a function that returns a channel.
;; The channel is running the go block defined in the function;
;; thus the channel is a handle on a service.
;; We use this pattern throughout the examples.
(defn copier
"A process that copies values from the source channel"
[source]
(let [c (chan)]
(go
(loop []
(let [value (<! source)]
;; this value will be nil if close! has been called on the channel.
;; we cannot copy nil to c because explicitly putting a nil is not allowed.
(if (nil? value)
(close! c)
(do
(>! c value)
(recur))))))
c))
(defn test-copy
"Print out all the numbers from 0 to 9,
then after two seconds print out the numbers from 10 to 19"
[]
(let [west (chan)
;; this process will remain ready to copy...
east (copier west)
;; a channel that will close after 2000 ms:
timeout (timeout 2000)]
(go
(dotimes [i 10]
(>! west i))
;; the only value that will come from the timeout is the nil when it closes:
(<! timeout)
(dotimes [i 10]
(>! west (+ 10 i))))
;; this process will remain ready to print...
(go
(loop []
(println (<! east))
(recur))))
;; until all the processes go away when they go out of scope:
nil)
(defn test-copy-and-close
"Print out all the numbers from 0 to 9,
then fail to print out the numbers from 10 to 19"
[]
(let [west (chan)
east (copier west)]
(go
(dotimes [i 10]
(>! west i))
(close! west)
(dotimes [i 10]
(>! west (+ 10 i))))
;; since west has closed, copier will close east;
;; once east has closed, we will forever get nils from it;
;; we never see the values put to west after west has closed;
;; they never even made it onto west
(go-times [i 20]
(println (<! east))))
nil)
;; 3.2 SQUASH
(defn squasher
"Copy from source channel but replace every pair of consecutive asterisks '**' by an upward arrow '^'.
Deal sensibly with input which ends with an odd number of asterisks."
[source]
(let [c (chan)]
(go
(loop []
(let [value (<! source)]
(cond
(nil? value) (close! c)
(not= value \*) (do
(>! c value)
(recur))
:else (let [value (<! source)]
(cond
(nil? value) (do
(>! c \*)
(close! c))
(not= value \*) (do
(>! c \*)
(>! c value)
(recur))
:else (do
(>! c \^)
(recur))))))))
c))
(defn chars>!
"Send all the chars of string to channel, then (if close is specified) signal completion by closing channel"
([channel string]
(chars>! channel string nil))
([channel string close]
(go
(dotimes [i (.length string)]
(>! channel (.charAt string i)))
(when-not (nil? close)
(close! channel)))))
(defn print-chars<!
"Dump everything from channel to stdout until channel closes, then print a newline"
[channel]
(go
(loop []
(let [value (<! channel)]
(if (nil? value)
(println "")
(do
(.print System/out value)
; This is apparently different from (print value), which doesn't flush everything until the user hits return!
(recur)))))))
(defn test-squasher-with
[string]
(let [west (chan)
east (squasher west)]
(chars>! west string :close)
(print-chars<! east))
nil)
(defn test-squash
"Print out 'here^^there*p^*'"
[]
(test-squasher-with "here****there*p***"))
;; 3.3 DISASSEMBLE
(def cardimage-without-first-char "1234567891123456789212345678931234567894123456789512345678961234567897123456789")
(defn cardimage [digit]
(str digit cardimage-without-first-char))
(defn disassembler
"Read cards from cardfile and output the stream of characters they contain.
An extra space should be inserted at the end of each card."
[cardfile]
(let [c (chan)]
(go
(loop []
(let [cardimage (<! cardfile)]
(if (nil? cardimage)
(close! c)
(do
(dotimes [i 80]
(>! c (.charAt cardimage i)))
(>! c \space)
(recur))))))
c))
(defn test-disassemble
"Dump cardimages 0 through 9 to stdout with a space after each"
[]
(let [cardfile (chan)
disassembler (disassembler cardfile)]
(go-times [i 10]
(>! cardfile (cardimage i)))
(print-chars<! disassembler))
nil)
;; 3.4 ASSEMBLE
(defn assembler
"Read a stream of characters from source channel and print them in lines of 125 characters, as on a lineprinter.
The last line should be completed with spaces (or pad-char, if specified) if necessary."
([source]
(assembler \space source))
([pad-char source]
(let [lineprinter (chan)]
(go
(loop []
(loop [lineimage ""]
(let [i (.length lineimage)
char (<! source)]
(cond
(nil? char) (do
(>! lineprinter (apply str lineimage (repeat (- 125 i) pad-char)))
(>! lineprinter \newline)
(close! lineprinter))
(< i 125) (recur (str lineimage char))
:else (do
(>! lineprinter lineimage)
(>! lineprinter \newline)
(recur "")))))))
lineprinter)))
(defn test-assemble
"Dump cardimages 0 through 9 to stdout in lines of 125 characters.
Pad the last line with Xs to 125 characters."
[]
(let [cardfile (chan)
lineprinter (assembler \X cardfile)]
(chars>! cardfile (apply str (for [i (range 10)] (cardimage i))) :close)
(print-chars<! lineprinter))
nil)
;; 3.5 REFORMAT
;; That GENERATOR pattern totally simplifies the composition of processes!
;; We just use a threading macro!
(defn reformatter
"Read a sequence of cards of 80 characters each, and print the characters on a lineprinter at 125 characters per line.
Every card should be followed by an extra space, and the last line should be completed with spaces if necessary.
...
This elementary problem is difficult to solve elegantly without coroutines."
([cardfile]
(reformatter \space cardfile))
([pad-char cardfile]
(->> cardfile
disassembler
(assembler pad-char))))
(defn reformatter-2
"Same as reformatter but with a copier coroutine in the middle"
([cardfile]
(reformatter-2 \space cardfile))
([pad-char cardfile]
(->> cardfile
disassembler
copier
(assembler pad-char))))
(defn test-reformatter [reformatter]
(let [cardfile (chan)
reformatter (reformatter \X cardfile)]
(go
(dotimes [i 10]
(>! cardfile (cardimage i)))
(close! cardfile))
(print-chars<! reformatter))
nil)
(defn test-reformat []
(test-reformatter reformatter))
(defn test-reformat-2 []
(test-reformatter reformatter-2))
;; 3.6 CONWAY'S PROBLEM
;; Not that Conway!
(defn conway
"Adapt the reformatter to replace every pair of consecutive asterisks by an upward arrow."
([cardfile]
(conway \space cardfile))
([pad-char cardfile]
(->> cardfile
disassembler
squasher
(assembler pad-char))))
(defn cardimage-with-stars
"Replace one random character in cardimage i with a pair of asterisks"
[i]
(let [cardimage (cardimage i)
index (int (+ 1 (rand 79)))
front (.substring cardimage 0 (- index 1))
back (.substring cardimage index)]
(str front "**" back)))
(defn test-conway []
(let [cardfile (chan)
conway (conway \X cardfile)]
(go
(dotimes [i 10]
(>! cardfile (cardimage-with-stars i)))
(close! cardfile))
(print-chars<! conway))
nil)