Search code examples
kdb

KDB namespace of a function


I have a function

f:{[t;c];
    q: (1.0;0.8;0.6;0.4;0.2);
    tc:(cols[t] where cols[t] like c)[0];
    sorted:desc t[tc];
    qt:( {x: "i"$sorted["i"$x*count sorted]} each q) ! q;
    k:asc key qt;
    {[t;c;n;f] @[t;n;:;f t c]}[t;c;`quint;{[x] qt k k binr "i"$x}]
    }

when I try to call it, it returns ERROR:sorted. It seems the variables declared inside the function are not accessible in {x: "i"$sorted["i"$x*count sorted]}. However, if I declare them outside the function then they are accessible (and I can call the function successfully). Is there a way to make them available to the {...}?


Solution

  • To access sorted you can pass it in as a argument to the function.

    {"i"$x["i"$y*count x]}[sorted]each q
    

    Same applies to the second lambda and on the last line too, again passing the arguments qt and k to it:

    {x y y binr "i"$z}[qt;k]