itertools.accumulate implementation
* itertools.accumulate implementation
Does not work with pickling, hits an issue with the special handling for reduce
* Combine accumulate implementations
@@ -84,6 +84,35 @@ namespace IronPython.Modules {
#endregion
}
+ [PythonType]
+ public class accumulate : IterBase {
+ public accumulate(CodeContext/*!*/ context, object iterable, object func = null) {
+ InnerEnumerator = Accumulator(context, PythonOps.GetEnumerator(iterable), func);
+ }
+
+ private IEnumerator<object> Accumulator(CodeContext/*!*/ context, IEnumerator iterable, object function) {
+ if (!MoveNextHelper(iterable)) {
+ yield break;
+ }
+
+ object total = iterable.Current;
+ yield return total;
+
+ if (function == null) {
+ PythonContext pc = context.LanguageContext;
+ while (MoveNextHelper(iterable)) {
+ total = pc.Add(total, iterable.Current);
+ yield return total;
+ }
+ } else {
+ while (MoveNextHelper(iterable)) {
+ total = PythonCalls.Call(function, total, iterable.Current);
+ yield return total;
+ }
+ }
+ }
+ }
+
[PythonType]
public class chain : IterBase {
private chain() { }
