/
itr.py
319 lines (263 loc) · 7.45 KB
/
itr.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
from historical_context import HistoricalContext
from collections import deque
import itertools
from acitertools import iter_memo, empty, no_more, needs, last
"""
A general purpose wrapper around iterables
Between acitertools.py and itr.py,
all functionality from itertools can be found.
Intended to provide all the higher order array methods that
JS and underscoreJS provide.
.filter() is seen as more general than
ifilter
and
ifilterfalse
from itertools.
To get the functionality of ifilterfalse,
use the Not class from filter.py
"""
# the name of this class is an exception to the convention because
# this is intended to be used as a function of sorts
def itr(iterable,remember_all=False):
if isinstance(iterable,_itr):
return iterable
else:
return _itr(iterable,remember_all)
def count(*args):
return itr(itertools.count(*args))
def repeat(*args):
return itr(itertools.repeate(*args))
class _itr(object):
"""
with this, one can do things like:
for x in itr(iterable).whatever():
stuff
rather than having to do:
for y in interable:
x = y.whatever()
stuff
and the calling of methods in a map() style
can keep on going
"""
def __init__(self,anIterable,remember_all=False):
"""
will use iter_memo to ensure that the iterable can restart,
but allow caller to indicate whether to set remember_all or not
"""
self.axon = iter_memo(iter(anIterable),remember_all)
self.iterable = self.axon.__iter__()
def copy(self):
"""
copies everything, not just what is left in the iterable,
if remember_all had been set in the constructor
"""
return itr(iter(self.axon.__iter__()))
def __getattr__(self,name):
return itr(self.getattr_core(name))
def getattr_core(self,name):
for item in self.iterable:
yield getattr(item,name)
def __getitem__(self,index):
"""
slices on self,
but indexes on elements.
May want to revisit this.
"""
if isinstance(index,slice):
return itr(itertools.islice(self.iterable,index.start,index.stop,index.step))
else:
return itr(self.getitem_core(index))
def getitem_core(self,index):
for item in self.iterable:
yield item[index]
def __call__(self,*args,**keywords):
return itr(self.call_core(*args,**keywords))
def call_core(self,*args,**keywords):
for item in self.iterable:
yield item(*args,**keywords)
def dropwhile(self,pred):
return itr(itertools.dropwhile(pred,self.iterable))
def takewhile(self,pred):
return itr(itertools.takewhile(pred,self.iterable))
def groupby(self,keyfunc=None):
return itr(itertools.groupby(self.iterable),keyfunc)
def tee(self,k=2):
return tuple(itr(self.axon.dendrite()) for i in range(k))
def cycle(self):
return itr(itertools.cycle(self.iterable))
def enumerate(self):
return itr(enumerate(self.iterable))
def numbered(self):
return self.enumerate()
def permutations(self,r=None):
return itr(itertools.permutations(self.iterable,r))
def combinations(self,r):
return itr(itertools.combinations(self.iterable,r))
def combinations_with_replacement(self,r):
return itr(itertools.combinations_with_replacement(self.iterable,r))
def k_wise(self,k):
return itr(self.k_wise_core(k))
def k_wise_core(self,k):
so_far = []
for item in self.iterable:
so_far.append(item)
if len(so_far) == k:
yield tuple(so_far)
so_far = []
if len(so_far) > 0:
while len(so_far) < k:
so_far.append(None)
yield tuple(so_far)
def filter(self,the_filter=lambda x: x):
return itr(self.filter_core(the_filter))
def filter_core(self,the_filter=lambda x: x):
for item in self.iterable:
if the_filter(item):
yield item
def cross(self,iterable):
return itr(self.cross_core(iterable))
def cross_core(self,iterable):
for x in self.iterable:
for y in iterable:
yield (x,y)
def map(self,the_callable):
return itr(self.map_core(the_callable))
def map_core(self,the_callable):
for item in self.iterable:
yield the_callable(item)
def unique(self):
return itr(self.unique_core())
def unique_core(self):
r = set()
for item in self.iterable:
if item in r:
continue
else:
r.add(item)
yield item
def group_while(self,pred,group_op=lambda x:x):
return itr(self.group_while_core(pred,group_op))
def group_while_core(self,pred,group_op):
iterable = self.iterable
groups = HistoricalContext(deque)
for item in iterable:
if len(groups.current) == 0 or pred(item,groups.current):
groups.current.append(item)
else:
groups.next.append(item)
group_op(groups.current)
yield groups.current
groups.new_next()
if not empty(groups.current): yield groups.current
def sorted_runs(self,sort_by):
def sorter(x,y):
return sort_by(x,last(y))
return self.group_while(sorter)
def first(self):
"just returns the first item"
for item in self.iterable:
return item
# if empty iterable, will raise an error, which is exactly what we want
def iteriter(self):
return itr(itertools.chain.from_iterable(self.iterable))
def iteriteriter(self):
return itr(self.iteriteriter_core())
def iteriteriter_core(self):
iterable = self.iterable
for item in itertools.chain.from_iterable(iterable):
for j in item:
yield j
def pcn_iter(self):
return itr(self.pcn_iter_core())
def pcn_iter_core(self):
"""
yields triples of (previous,current,next)
for every item in iterable,
with None if there is no previous or next
"""
iterable = self.iterable
h = HistoricalContext()
yielded_anything = False
for item in iterable:
h.set_next(item)
if h.current is not None:
yield h.pcn_tuple()
yielded_anything = True
h.new_next()
result = h.pcn_tuple()
(p,c,n) = result
if c is not None:
yield result
def merge(self,with_what):
return itr(self.merge_core(with_what))
def merge_core(self,with_what):
"treat them both as sorted iterables and merge them together"
A = iter(self.iterable)
B = iter(with_what)
try:
a,b = next(A),next(B) # they must have at least one in each
while True:
while a < b:
with needs.A: yield a; a = next(A)
with needs.B: yield b; b = next(B)
except no_more.A:
yield b
for b in B: yield b
except no_more.B:
yield a
for a in A: yield a
def balanced_reduce(self,func):
iterable = self.iterable
d = deque(iterable)
while len(d) > 1:
x,y = d.pop(),d.pop()
d.appendleft(func(x,y))
assert(len(d) == 1)
return d.pop()
def __iter__(self):
for item in self.iterable:
yield item
def dict_of_lists_from(self,key_func=lambda x:x,value_func=lambda x:x):
return _dict_of_lists_from(key_func = key_func, value_func = value_func, items = self)
def print_on_get(self,f=lambda x:x):
for item in self:
print f(item)
yield item
from collections import defaultdict
class _dict_of_lists_from(defaultdict):
def __init__(self,items,key_func=lambda x:x,value_func=lambda x:x):
super(_dict_of_lists_from,self).__init__(list)
for item in items:
self[key_func(item)].append(value_func(item))
""" this way, we can do the following:
from itr import itr
for x in itr(...).whatever().whatever()...:
...
"""
if __name__ == "__main__":
def fib(n):
if n < 2:
return n
else:
return fib(n-1)+fib(n-2)
class sFib(object):
def __init__(self):
self.im = itr(self).unique()
def __iter__(self):
a = 0
b = 1
yield b
while 1:
a = a+b
yield a
b = a+b
yield b
def iterator(self):
for r in self.im:
yield r
s = sFib()
j0 = s.iterator()
for i in range(10):
j1 = next(j0)
j2 = fib(i+1)
print (j1,j2)