diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 71c7ddc0..429f6c30 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -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) diff --git a/toolz/tests/test_itertoolz.py b/toolz/tests/test_itertoolz.py index e0a8d17f..8a5c2407 100644 --- a/toolz/tests/test_itertoolz.py +++ b/toolz/tests/test_itertoolz.py @@ -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]