How does one apply a function to every element of an array in kdb? For example, to square an array of numbers in various languages:
javascript: [1,2,3,4].map(square) -> [1,4,9,16]
python: [square(x) for x in [1,2,3,4]] -> [1,4,9,16]
kdb: {[x] x*x} (1;2;3;4) -> 1 4 9 16
So I thought I'd write another function that takes a date and returns the name of a CSV file for that date:
q)gobble:{[x] string[x-1970.01.01],".csv"}
q)gobble .z.d // .z.d == today
"16781.csv"
Hurrah. So now I should be able to apply that function to an array of dates and get an array of strings in response, thought I. Foolish mortal:
q)gobble .z.d + til 5 // .z.d + til 5 == array of 5 days starting today
"16781"
"16782"
"16783"
"16784"
"16785"
"."
"c"
"s"
"v"
Obviously squaring in kdb was a special case of some kind, not to be generalised to all functions. How can I tell KDB to apply an arbitrary function to each date in turn, pretty please? Many thanks!
See each
q)gobble each .z.d + til 5
"16782.csv"
"16783.csv"
"16784.csv"
"16785.csv"
"16786.csv"
The square operation worked as *
can operate on atoms as well as lists
q)1 2 3 4 * 1 2 3 4
1 4 9 16