Home › Forums › kdb+ › How to use scan to obtain cumulative value of a function › Reply To: How to use scan to obtain cumulative value of a function
-
Ok so you are saying that you need to cary forward distinct values and apply additional rules to the result? For this you could try using the form v[x;y;z], see https://code.kx.com/q/ref/accumulators/#ternary-values
This will allow you to pass in the columns filtered_levels, low and high when accumulating, so that you can apply additional rules. Below is a simple example using a mock table to demonstrateq)t:([] filtered_levels:((5.0;6.0;8.0;12.0;13.0);(7.0;10.0;11.0);(4.0;5.0;7.0;8.0;9.0)); low:5.0 7.0 4.0;high:13.0 11.0 9.0 )
q)v:{[x;f;l;h] c:distinct x,f;c where c within (l;h)}
q)update cumulative:v[();filtered_levels;low;high] from t
Here the “v” function is not just joining consecutive filtered_levels (and taking the distinct), but also applying logic to the result using low and high values.
code.kx.com
Accumulators – Reference – kdb+ and q documentation - kdb+ and q documentation
An accumulator is an iterator that takes an applicable value as argument and derives a function that evaluates the value, first on its entire (first) argument, then on the results of successive evaluations.