KDB/q pass two list of arguments to a function

120 Views Asked by At

Hi recently started learning KDB, I am trying to performing something like below, would appreciate your help with simplifying it.

q)f:{[x;dt] flip `Destination`Session`Time!(3#last ` vs x;`start`mid`end;("P"$(string dt),"D","01:15:00.000000000";"P"$(string dt),"D","01:30:00.000000000";"P"$(string dt),"D","06:57:00.000000000")) }

q)dt:.z.d+til 5
q)f1:{f[x;y]} each `AAA`BBB`CCC`DDD
q)raze{raze f1[x]} each dt
2

There are 2 best solutions below

1
Thomas Smyth - Treliant On

Can use the each glyph ' for this:

raze raze f'[`AAA`BBB`CCC`DDD]'[dt]
0
Cathal O'Neill On

If the order of the table doesn't matter, here's another alternative

q)x:`AAA`BBB`CCC`DDD
q)ses:`start`mid`end
q)dt:2023.10.31 2023.11.01 2023.11.02 2023.11.03 2023.11.04
q)tms:01:15 01:30 06:57
q)ungroup flip`Destination`Session`Time!flip x cross ses,'enlist each dt+/:tms
Destination Session Time
-------------------------------------------------
AAA         start   2023.10.31D01:15:00.000000000
AAA         start   2023.11.01D01:15:00.000000000
AAA         start   2023.11.02D01:15:00.000000000
AAA         start   2023.11.03D01:15:00.000000000
AAA         start   2023.11.04D01:15:00.000000000
AAA         mid     2023.10.31D01:30:00.000000000
AAA         mid     2023.11.01D01:30:00.000000000
AAA         mid     2023.11.02D01:30:00.000000000
AAA         mid     2023.11.03D01:30:00.000000000
AAA         mid     2023.11.04D01:30:00.000000000
AAA         end     2023.10.31D06:57:00.000000000
AAA         end     2023.11.01D06:57:00.000000000
AAA         end     2023.11.02D06:57:00.000000000
AAA         end     2023.11.03D06:57:00.000000000
AAA         end     2023.11.04D06:57:00.000000000
..

Worth noting as well, there's no need to create the timestamps in your table using strings

f:{[x;dt] flip `Destination`Session`Time!(3#last ` vs x;`start`mid`end;("P"$(string dt),"D","01:15:00.000000000";"P"$(string dt),"D","01:30:00.000000000";"P"$(string dt),"D","06:57:00.000000000")) }

is equivalent to

f:{[x;dt] flip `Destination`Session`Time!(3#x;`start`mid`end;(dt+01:15 01:30 06:57))}