Search code examples
kdb

Usage of apply (@) to create sym vector manually in kdb


Reference: https://code.kx.com/q4m3/14_Introduction_to_Kdb+/#1422-splayed-tables-with-symbol-columns

When doing the enumeration manually, can someone please explain how is sym vector created using @ in below example?

Example:
I have opened a new q instance:

q)t:([] s1:`a`b`c; v:10 20 30; s2:`x`y`z)
q)@[t;exec c from meta t where "s"=t;`sym?] /- Creates a sym vector in memory
q)sym
/- Output - `a`b`c`x`y`z
q)type sym
/- Output - 11h

EDIT-
Understood, it is apply at which is doing the job, it is equal to updating a list using apply at. Eg:

q)l:2 3 9
q)@[l;1;neg]
2 -3 9

Similarly we are applying enum extend to sym vector based on the condition as second argument i.e all sym columns of table t

@[t;exec c from meta t where t="s";`sym?]

Solution

  • The @ (amend) operator is not what generates the `sym vector, it's the ? (Enum Extend) operator that does so.

    @ above applies the `sym? to all columns of t which are symbol types. You could instead apply it directly yourself and generate `sym:

    q)show t:([] s1:`a`b`c; v:10 20 30; s2:`x`y`z)
    s1 v  s2
    --------
    a  10 x
    b  20 y
    c  30 z
    q)sym
    'sym
      [0]  sym
           ^
    q)`sym?t`s1
    `sym$`a`b`c
    q)sym
    `a`b`c
    q)`sym?t`s2
    `sym$`x`y`z
    q)sym
    `a`b`c`x`y`z
    

    `sym is created when we first call the function, and continues to be extended whenever we call it again.