Skip to content

Commit 2bf9e8b

Browse files
authored
Merge pull request #410 from groutr/fix-accumulate-3.7
Fix accumulate for Python 3.7
2 parents c789e3c + a69f8a5 commit 2bf9e8b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

toolz/itertoolz.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ def accumulate(binop, seq, initial=no_default):
5656
itertools.accumulate : In standard itertools for Python 3.2+
5757
"""
5858
seq = iter(seq)
59-
result = next(seq) if initial == no_default else initial
59+
if initial == no_default:
60+
try:
61+
result = next(seq)
62+
except StopIteration:
63+
return
64+
else:
65+
result = initial
6066
yield result
6167
for elem in seq:
6268
result = binop(result, elem)

toolz/tests/test_itertoolz.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ def binop(a, b):
289289

290290
start = object()
291291
assert list(accumulate(binop, [], start)) == [start]
292+
assert list(accumulate(binop, [])) == []
292293
assert list(accumulate(add, [1, 2, 3], no_default2)) == [1, 3, 6]
293294

294295

0 commit comments

Comments
 (0)