Is there a better way to calculate a moving sum of a list?
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
int i = 0;
int maxLength = data.length - window + 1;
while (i < maxLength) {
List tmpData = data.getRange(i, i + window).toList();
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
i++;
}
// filling the first n values with null
i = 0;
while (i < window - 1) {
sum.insert(0, null);
i++;
}
return sum;
}
2
Answers
Well, the code is already clean for what you need. Maybe just some improvements like:
for loop
sublist
which creates a "view" of a list, which is more efficientpadLeft
, where you specify the lenght of the list which you want it to become (first parameter), then the value you want to use to fill it (second parameter). For example, if you have an array of N elements, and you want to fill it with X "null"s to the left, usepadLeft(N+X, null)
.if I understand your problem correctly you can just calculate the window one time and in one loop you can for each iteration you can add the current element to the sum and subtract
i - (window - 1)
so for an input like this
data
= [1,2,3,4,5,6]window
= 3the below code will result in [6,9,12,15]
this way you won’t have to use
getRange
norsublist
norreduce
as all of those are expensive functions in terms of time and space complexity