Search code examples
flutterlistdartsum

Is there a better way to calculate the moving sum of a list in flutter


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;
}

Solution

  • 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 = 3

    the below code will result in [6,9,12,15]

    int sum = 0;
    List<double> res = [];
    for (int i = 0;i<data.length;i++) {
         sum += data[i];
         if (i < window - 1) {
             continue;
         }
         res.add(sum);
         sum -= data[i - (window - 1)]; // remove element that got out of the window size
     }
         
    

    this way you won't have to use getRange nor sublist nor reduce as all of those are expensive functions in terms of time and space complexity