Over / Scan puzzles in Q
When reading Q Tips there is an introduction about Over / Scan using an example of computing EWMA (exponentially weighted moving average):
Note that for the inner function, we should read it from right side to the left side:
The formula for EWMA calculation is conceptually simple:
EWMA(t) = Weight * X(t) + (1 - Weight)*EWMA(t-1)
while to understand the example in q is really brain burning.
First the difference between over (/) and scan() is that, the previous one will only return the final result nevertheless for the latter one each of the intermediate values will be returned. And
scan is used in this example.
q)ewma[.25] 1 2 3 4 5f 1 1.25 1.6875 2.265625 2.949219
In the example above, [x] is an atom of [.25] and [y] is a vector of (1;2;3;4;5f). The
scan operator will change a dyadic(two parameter) function into a monadic function that takes a single list of values, and each call to the modified function will take the previous value as the first argument and the second argument will be the next value from the list. And if the initial value is not given, the first value of the list after the
scan operator will be offered as the initial value, in the meanwhile the other params will be set to 0.
Base on the explanation, the passed arguments will be
which matches the result of
ewma[.25] 1 2 3 4 5f.
More to read: Accumulators.