How can express this imperative function in a functional, array-based language like K (or Q)? -
How can express this imperative function in a functional, array-based language like K (or Q)? -
how can express imperative function in functional, array-based language k (or q)?
in sloppy c++:
vector<int> x(10), y(10); // assume these initialized values. // btw, 4 const -- it's part of algorithm , arbitrarily chosen. vector<int> result1(x.size() - 4 + 1); // place hold resulting array. vector<int> result2(x.size() - 4 + 1); // place hold resulting array. // here's code want express functionally. (int = 0; <= 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 see in kdb , q other functional languages welcome.
in kona (an open-source k dialect):
first, set illustration values (using same clojure solution):
a:1+!8;b:8#0 / 1..8, b 8 0s
then:
{(|/x;&/x)}@+{4#y _ x}[a+b;]'!#a
where a , b x , y variables above. (k makes special case variables x, y, , z.)
to break bit more:
maxmin:{(|/x;&/x)} / (max;min) pairs of x get4:{4#y _ x} / next 4 x, starting @ y / <4 remaining, repeat; doesn't matter min or max / maxmin applied flipped results of get4(a-b) @ each index 0..(length a)-1 maxmin@+get4[a-b;]'!#a / result (4 5 6 7 8 8 8 8 1 2 3 4 5 6 7 8)
functional-programming k kdb
Comments
Post a Comment