I want to iterate over all the rows in a time series, partitioned kdb database and perform some calculation at each step. In SQL this could be done with a cursor. Is there something similar in kdb?
Some background: the database consist of several million time series records and I need to maintain a state for a number of objects as I progress through the rows. There are a few 'if' statements I need to run at each step. I thought to integrate my calculation in a query, but because of the number of tests I have to perform I think a cursor or something like a row callback would be more appropriate. What's the most efficient solution to run custom code while "replaying" the database?
Tables in q behave much like lists of dictionaries. So if t is a table, t[0] would be a dictionary mapping column names to the first row of values, t[1] would be the second row, and so on. So in effect your cursor is just an index and progressing the cursor is just incrementing the index.
On the other hand, probably the more idiomatic way to apply an operation to a list element-wise is with the each operator: