KDB Gurus,
I want to limit the number of “fills” to n time. I came up with: { i::0; { $[not[null[z]] | i>=x;[i::0;z];[i::i+1; y]]} [y;z]}[2][1;2 0N 0N 0N 3 0N 0N 0N 4 5], which is taking more than 3 times the “fills” operation.
Can anyone please share a better approach to this. TIA.
User7
September 15, 2020, 11:35pm
2
Is something like this what you’re looking for?
q)show r:2{prev ^x}/2 0N 0N 0N 3 0N 0N 0N 4 5
2 2 2 0N 3 3 3 0N 4 5
q)r~{i::0;{$[not[null[z]]|i>=x;[i::0;z];[i::i+1;y]]} [y;z]}[2][1;2 0N 0N 0N 3 0N 0N 0N 4 5]
1b
q)\t:50000 fills 2 0N 0N 0N 3 0N 0N 0N 4 5
185
q)\t:50000 2{prev ^x}/2 0N 0N 0N 3 0N 0N 0N 4 5
266
Regards,
Cillian
| | Virus-free. www.avg.com |
Try:
q)list:2 0N 0N 0N 3 0N 0N 0N 4 5 q)fill:{prev ^x} q)fill/[2; list] 2 2 2 0N 3 3 3 0N 4 5
Can also move “/” in fill itself
q)fill:^':/
q)fill[2;l]
2 2 2 0N 3 3 3 0N 4 4 4
This seems to be faster if n is greater than ~10 (on my machine):
q) list:2 0N 0N 0N 3 0N 0N 0N 4 5
q) ccr:{sm-fills ?[nl;first 0#x;sm:sums nl:null x]}
q) ccr list
0 1 2 3 0 1 2 3 0 0
q) f:{[n;l] ?[(n|0)>=ccr[l];fills l; first 0#l]}
q) f[2;list]
2 2 2 0N 3 3 3 0N 4 5