Search code examples
kdb

What is (!). in kdb and are below usecases valid to use it?


  1. What is (!). called in kdb?

  2. and are below use cases valid to use (!). to convert a list to a dictionary or are there better ways and other uses of (!). ?
    Example:

    q)(!). (`A`B;(`C`D`E;`F`G`H));
    q).[(!);flip (`A`B;`C`D;`E`F)]
    

I cannot find any documentation on the use cases on (!). in kdb tutorials. Please share any information on (!). and its uses?


Solution

  • It's a version of apply & yep your use case is valid. The reason the operator is wrapped in parentheses is because it itself is a dyadic infix operator as is dot apply (.)

    If you attempt to apply it as is, your expression is like so, which Q doesn't like

    // infixOp infixOp operand
    q)+ . 4 5
    '
      [0]  + . 4 5
            ^
    

    Wrapping the operator within parentheses effectively transforms it so the expression now becomes

    // operand infixOp operand
    q)(+). 4 5
    9
    

    If you define a function which can't be used infix, then there's no need to wrap it

    q)f:+
    q)4 f 5
    'type
      [0]  4 f 5
           ^
    q)f . 4 5
    9
    

    If using apply with bracket notation as in your example, there's no need to wrap the function

    q).[+;4 5]
    9
    

    https://code.kx.com/q/ref/apply/#apply-index

    https://code.kx.com/q/basics/syntax/#parentheses-around-a-function-with-infix-syntax

    Jason