@@ -527,29 +527,51 @@ class accumulate(object):
527
527
528
528
Return series of accumulated sums."""
529
529
530
+ _marker = object ()
531
+
530
532
@__graalpython__ .builtin_method
531
- def __init__ (self , iterable , func = None ):
533
+ def __init__ (self , iterable , func = None , * , initial = None ):
532
534
self .iterable = iter (iterable )
533
535
self .func = func
534
- self .total = None
536
+ self .total = accumulate ._marker
537
+ self .initial = initial
535
538
536
539
@__graalpython__ .builtin_method
537
540
def __iter__ (self ):
538
541
return self
539
542
540
543
@__graalpython__ .builtin_method
541
544
def __next__ (self ):
545
+ if self .initial is not None :
546
+ self .total = self .initial
547
+ self .initial = None
548
+ return self .total
542
549
value = next (self .iterable )
543
- if self .total is None :
550
+ if self .total is accumulate . _marker :
544
551
self .total = value
545
552
return value
546
553
547
554
if self .func is None :
548
- self .total += value
555
+ self .total = self . total + value
549
556
else :
550
557
self .total = self .func (self .total , value )
551
558
return self .total
552
559
560
+ @__graalpython__ .builtin_method
561
+ def __reduce__ (self ):
562
+ if self .initial is not None :
563
+ it = chain ((self .initial ,), self .iterable )
564
+ return type (self ), (it , self .func ), None
565
+ elif self .total is None :
566
+ it = accumulate (chain ((self .total ,), self .iterable ), self .func )
567
+ return islice , (it , 1 , None )
568
+ else :
569
+ return type (self ), (self .iterable , self .func ), self .total
570
+
571
+ @__graalpython__ .builtin_method
572
+ def __setstate__ (self , state ):
573
+ self .total = state
574
+
553
575
554
576
class dropwhile (object ):
555
577
"""
0 commit comments