4

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]

zrb
  • 851
  • 7
  • 16

1 Answers1

4
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
    -----------------------------
MdSalih
  • 1,978
  • 10
  • 16
  • 1
    There is one issue with this approach. It can create duplicate columns which is not possible in normal syntax. For ex f[`d`d;"sd"] will create two 'd' columns whereas normal syntax will rename other to 'd1'. To fix this, one can insert a duplicate check or renaming logic in the function. Other option is to use and create only distinct columns from the input like: { flip (distinct x)# x!y$\:()} . This will consider the first datatype of a duplicate column. – Rahul Feb 07 '16 at 17:06
  • Probably worth mentioning also that string columns would have to be generated using the latter method with "\*" as the datatype, e.g. ct[\`col1\`col2;"j\*"] – terrylynch Feb 08 '16 at 18:14