How to express this imperative function in a functional, array-based language, such as K (or Q )?
In sloppy C ++:
vector<int> x(10), y(10); // Assume these are initialized with some values. // BTW, 4 is just a const -- it part of the algorithm and is arbitrarily chosen. vector<int> result1(x.size() - 4 + 1); // A place to hold a resulting array. vector<int> result2(x.size() - 4 + 1); // A place to hold another resulting array. // Here the code I want to express functionally. for (int i = 0; i <= x.size() - 4; i++) { int best = x[i + 0] - y[i + 0]; int bad = best; int worst = best; for(int j = 0; j < 4; j++) { int tmp = x[i + j] - y[i + 0]; bad = min(bad, tmp); if(tmp > best) { best = tmp; worst = bad; } } result1[i] = best result2[i] = worst }
I would like to see this in kdb and Q, but welcome other functional languages.
source share