Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def accumulate(binop, seq, initial=no_default):
itertools.accumulate : In standard itertools for Python 3.2+
"""
seq = iter(seq)
result = next(seq) if initial == no_default else initial
if initial == no_default:
try:
result = next(seq)
except StopIteration:
return
else:
result = initial
yield result
for elem in seq:
result = binop(result, elem)
Expand Down
1 change: 1 addition & 0 deletions toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def binop(a, b):

start = object()
assert list(accumulate(binop, [], start)) == [start]
assert list(accumulate(binop, [])) == []
assert list(accumulate(add, [1, 2, 3], no_default2)) == [1, 3, 6]


Expand Down