This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cotools.py
344 lines (223 loc) · 8 KB
/
cotools.py
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
"""
cotools is a collection of functional programming primitives utilizing the
cooperative multitasking facilities provided by the Twisted framework.
"""
from twisted.internet.task import coiterate
from twisted.internet.defer import maybeDeferred, succeed
class _CoFunCaller(object):
def __init__(self, function=None, stopFunction=None, resultCollector=None):
self._function = function
self._stopFunction = stopFunction
self._resultCollector = resultCollector
self._stopped = False
def _maybeStop(self, result):
if self._stopFunction(result):
self._stopped = True
def do(self, iterator):
for item in iterator:
d = maybeDeferred(lambda: item)
if self._function is not None:
d.addCallback(self._function)
if self._resultCollector is not None:
d.addCallback(self._resultCollector)
if self._stopFunction is not None:
d.addCallback(self._maybeStop)
yield d
if self._stopped:
return
def coiterate(self, iterator):
d = maybeDeferred(lambda: iterator)
d.addCallback(lambda i: coiterate(self.do(i)))
return d
def coforeach(function, iterator):
"""
Apply function to each item in iterator.
"""
return _CoFunCaller(function=function).coiterate(iterator)
def cofilter(function, iterator):
"""
Return items in iterator for which `function(item)` returns True.
"""
results = []
def checkFilter(notfiltered, item):
if notfiltered == True:
results.append(item)
def dofilter(item):
d = maybeDeferred(function, item)
d.addCallback(checkFilter, item)
return d
d = _CoFunCaller(resultCollector=dofilter).coiterate(iterator)
d.addCallback(lambda _: results)
return d
def comap(function, iterator):
"""
Applies function to each item in iterator returning a list of the return
values of `function(item)`.
"""
results = []
cfc = _CoFunCaller(function, resultCollector=results.append)
d = cfc.coiterate(iterator)
d.addCallback(lambda _: results)
return d
def cofold(function, initial, iterator):
"""
Calls `function(accumulator, item)` for each item in iterator using the
return value as the accumulator for the next item. `cofold` will return
the last accumulator.
"""
acc = [initial]
def handleAcc(newAcc):
acc[0] = newAcc
def dofold(item):
return function(acc[0], item)
d = _CoFunCaller(dofold, resultCollector=handleAcc).coiterate(iterator)
d.addCallback(lambda _: acc[0])
return d
def cosum(iterator):
"""
Sum all the items in iterator. Implemented as
`cofold(lambda a, b: a + b, 0, iterator)`.
More of a toy than a useful primitive.
"""
return cofold(lambda a, b: a + b, 0, iterator)
def cotakewhile(function, iterator):
"""
Take items found in iterator and return them until an item is found where
`function(item)` does not return true.
"""
results = []
def checkTake(shouldTake, item):
if shouldTake == True:
results.append(item)
return item
def dotake(item):
d = maybeDeferred(function, item)
d.addCallback(checkTake, item)
return d
def dostop(takeResult):
return takeResult is None
cfc = _CoFunCaller(resultCollector=dotake, stopFunction=dostop)
return cfc.coiterate(iterator).addCallback(lambda _: results)
#
# Unit tests
#
from twisted.trial import unittest
class CotoolsTests(unittest.TestCase):
def test_comap_deferred_iterator(self):
def _checkResult(result):
self.assertEquals(result, [0, 2, 4, 6, 8, 10])
d = comap(lambda x: x * 2, succeed([0, 1, 2, 3, 4, 5]))
d.addCallback(_checkResult)
return d
def test_comap(self):
def _checkResult(result):
self.assertEquals(result, [0, 2, 4, 6, 8, 10])
d = comap(lambda x: x * 2, [0, 1, 2, 3, 4, 5])
d.addCallback(_checkResult)
return d
def test_comap_deferred(self):
def _checkResult(result):
self.assertEquals(result, [2, 4])
d = comap(lambda x: x * 2, [succeed(1), succeed(2)])
d.addCallback(_checkResult)
return d
def test_comap_deferred_function(self):
def _checkResult(result):
self.assertEquals(result, [2, 4])
d = comap(lambda x: succeed(x * 2), [1, 2])
d.addCallback(_checkResult)
return d
def test_cofold(self):
def _checkResult(result):
self.assertEquals(result, 15)
d = cofold(lambda a, b: a + b, 0, [0, 1, 2, 3, 4, 5])
d.addCallback(_checkResult)
return d
def test_cofold_deferred(self):
def _checkResult(result):
self.assertEquals(result, 15)
d = cofold(lambda a, b: a + b, 0, [succeed(10), succeed(5)])
d.addCallback(_checkResult)
return d
def test_cofold_deferred_function(self):
def _checkResult(result):
self.assertEquals(result, 15)
d = cofold(lambda a, b: succeed(a + b), 0, [10, 5])
d.addCallback(_checkResult)
return d
def test_cosum(self):
def _checkResult(result):
self.assertEquals(result, 15)
d = cosum([0, 1, 2, 3, 4, 5])
d.addCallback(_checkResult)
return d
def test_coforeach(self):
results = []
def _handleItem(i):
results.append(i * 2)
def _checkResult(_):
self.assertEquals(results, [0, 2, 4, 6, 8, 10])
d = coforeach(_handleItem, [0, 1, 2, 3, 4, 5])
d.addCallback(_checkResult)
return d
def test_coforeach_deferred(self):
results = []
def _handleItem(i):
results.append(i * 2)
def _checkResult(_):
self.assertEquals(results, [2, 4])
d = coforeach(_handleItem, [succeed(1), succeed(2)])
d.addCallback(_checkResult)
return d
def test_coforeach_deferred_function(self):
results = []
def _handleItem(i):
results.append(i * 2)
return succeed(None)
def _checkResult(_):
self.assertEquals(results, [2, 4])
d = coforeach(_handleItem, [1, 2])
d.addCallback(_checkResult)
return d
def test_cofilter(self):
def _checkResult(result):
self.assertEquals(result, [0, 2, 4, 6, 8, 10])
d = cofilter(lambda x: x % 2 == 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
d.addCallback(_checkResult)
return d
def test_cofilter_deferred(self):
def _checkResult(result):
self.assertEquals(result, [2])
d = cofilter(lambda x: x % 2 == 0, [succeed(1), succeed(2)])
d.addCallback(_checkResult)
return d
def test_cofilter_deferred_function(self):
def _checkResult(result):
self.assertEquals(result, [2])
d = cofilter(lambda x: succeed(x % 2 == 0), [1, 2])
d.addCallback(_checkResult)
return d
def test_cotakewhile(self):
def _checkResult(result):
self.assertEqual(result, [0, 1, 2, 3, 4])
d = cotakewhile(lambda x: x < 5, [0, 1, 2, 3, 4, 5])
d.addCallback(_checkResult)
return d
def test_cotakewhile_deferred(self):
def _checkResult(result):
self.assertEquals(result, [1])
d = cotakewhile(lambda x: x < 2, [succeed(1), succeed(2)])
d.addCallback(_checkResult)
return d
def test_cotakewhile_deferred_function(self):
def _checkResult(result):
self.assertEquals(result, [1])
d = cofilter(lambda x: succeed(x < 2), [1, 2])
d.addCallback(_checkResult)
return d
def test_cosum_comap(self):
def _checkResult(result):
self.assertEquals(result, 30)
d = cosum(comap(lambda x: x * 2, [0, 1, 2, 3, 4, 5]))
d.addCallback(_checkResult)
return d