Search code examples
kdbq-lang

What is the q @ symbol doing in the context of being applied as a function call?


What is going on in the example here?

Source: http://code.kx.com/q/ref/select/#index-at

@[d;1 1 1;+;3]
((1 2 3;4 5 6 7);(17 18;19;20 21);(13 14;15 16 17 18;19 20))

Solution

  • http://code.kx.com/wiki/JB:QforMortals2/functions#Functional_Forms_of_Amend

    @[L;I;f;y]
    L=your list
    I=index of elements to modify
    f=function to apply
    y=2nd parameter to f

    You can see what it is doing by placing + in a function and adding some logging output. In this case it indexes into the 2nd element d@1, retrieves (8 9;10;11 12), adds 3, results in (11 12;13;14 15), uses that as it's next input, adds 3, results in (14 15;16;17 18).

        q)@[d;1 1 1;{0N!("x is:",.Q.s1 x;"y is:",.Q.s1 y);x+y};3]
        ("x is:(8 9;10;11 12)";"y is:3")
        ("x is:(11 12;13;14 15)";"y is:3")
        ("x is:(14 15;16;17 18)";"y is:3")
        (1 2 3;4 5 6 7)
        (17 18;19;20 21)
        (13 14;15 16 17 18;19 20)
    

    Or it can be seen using over(/):

        q)@/[d;;{0N!(x;y);x+y};3]1 1 1
        ((8 9;10;11 12);3)
        ((11 12;13;14 15);3)
        ((14 15;16;17 18);3)
        (1 2 3;4 5 6 7)
        (17 18;19;20 21)
        (13 14;15 16 17 18;19 20)