Search code examples
kdb

Overloading the operator in functional amend


How to overload an operator in functional amend?

s:string (`a1`b2`c3)
b:string til 2

using functional amend with , gives

q)@[s;0 2;,;b]
("a10";"b2";"c31")

I want to overload the , (append) to prefix the content of list b to list a like :

("0a1";"b2";"1c3")

Solution

  • You need to use a custom function {y,x} instead if , to achieve this

    @[s;0 2;{y,x};b]
    ("0a1";"b2";"1c3")
    

    Please note that here , is a dyadic function; Any other dyadic function e.g. {y,x} can be used in functional amend with valance 4.

    The general format of functional amend is following, where f is dyadic function

     @[L;I;f;y]
    
    q)@[1 2 3 4 ;1 3;*;5 ]   // * is dyadic function {x*y} 
    1j, 10j, 3j, 20j
    

    and when f is monadic function

     @[L;I;f]
    
    q)@[1 2 3 4 ;1 3;neg ]
    1j, -2j, 3j, -4j