I am trying to build up a time series using an autoregressive formula in Scala. The equation looks to the prior index of the target array and the current index of two other arrays. Example dummy equation:
z_i = z[i-1] - x[i] + y[i]
It is straightforward in an imperative style (see C++ example below) but I am trying to figure out the idiomatic way in Scala. I have created a solution with a while loop but it feels clunky and un-Scalaesque.
#include <vector>
#include <iostream>
int main() {
std::vector<int> x {1, 2, 3, 4};
std::vector<int> y {10, 9, 8, 7};
std::vector<int> z {0, 0, 0, 0};
for(int i = 1; i < x.size(); i++) {
z[i] = z[i-1] - x[i] + y[i];
}
for(auto i: z) {
std::cout << i << std::endl;
}
}
// Desired result
[0, 7, 12, 15]