Search code examples
kdbk

KDB/k: Functional form to create a table variable


Is there a functional form equivalent to the following?

trades:([]date:`date$();time:`time$();sym:`symbol$();price:`real$();size:`int$(); cond:`char$())

Assuming the function name is "ct"...

trades:ct[fieldNames;types]


Solution

  • ct:{[fields;types] flip fields!types$\:()}
    

    Example:

    q)ct[`date`time`sym`price`size`cond;`date`time`symbol`float`long`symbol]
        date time sym price size cond
        -----------------------------
    

    Will also work with char form of types:

    q)ct[`date`time`sym`price`size`cond;"dtsfjs"]
        date time sym price size cond
        -----------------------------