Skip to content

Commit c1c15d3

Browse files
committed
add itertools.accumulate as it is used in the random module
1 parent d50e7d0 commit c1c15d3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

graalpython/lib-graalpython/itertools.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,30 @@ def __setstate__(self, state):
305305
self.indices[i] = index
306306
lst.append(gear[index])
307307
self.lst = lst
308+
309+
310+
class accumulate(object):
311+
"""
312+
"accumulate(iterable) --> accumulate object
313+
314+
Return series of accumulated sums."""
315+
316+
def __init__(self, iterable, func=None):
317+
self.iterable = iter(iterable)
318+
self.func = func
319+
self.total = None
320+
321+
def __iter__(self):
322+
return self
323+
324+
def __next__(self):
325+
value = next(self.iterable)
326+
if self.total is None:
327+
self.total = value
328+
return value
329+
330+
if self.func is None:
331+
self.total += value
332+
else:
333+
self.total = self.func(total, value)
334+
return self.total

0 commit comments

Comments
 (0)