Iterate function over list of inputs (Q/KDB)

4.7k views Asked by At

I have a function, f[symbol;date0;date1] , as well as a range of dates, say

2017.12.04
2017.12.05
2017.12.06

I'd like to run this function for a given symbol - assume "AAPL" - once for each day. In essence:

f[AAPL;2017.12.04;2017.12.04]
f[AAPL;2017.12.05;2017.12.05]
f[AAPL;2017.12.05;2017.12.05]

This function returns a table, so I'd like each date to just be appended to the previous results. What might be the best way to do this?

2

There are 2 answers

0
Ryan McCarron On BEST ANSWER

The best approach for functions in this form is to use the kdb each-both in its generic form:

d:2017.12.04 2017.12.05 2017.12.06

f'[`AAPL;d;d]

kdb recognises the atomic first argument and list second argument, and applies the function to each in order. You can use raze to join each table in order as well:

raze f'[`AAPL;d;d]

then returns a single joined table, if the schema are the same.

0
Sean O'Hagan On

assuming f produces identical columns for each call then you can just raze the result

q)f:{([]1#x;1#y;1#z)};
q)raze f'[`a;3#.z.d;3#.z.d-1]
x y          z
-----------------------
a 2018.02.07 2018.02.06
a 2018.02.07 2018.02.06
a 2018.02.07 2018.02.06

the result is a [larger] table with the results appended to each other