KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ How to make two referencing sub-projection works?

  • How to make two referencing sub-projection works?

    Posted by vivo on July 29, 2022 at 12:00 am

    I have below code, which doesn’t work. h and g are 2 projections defined inside f, and they are referencing to each other.

    Theorectically, the answer should be:

    f[10]=g[10]

    =h[8] + 20

    =g[5] + 30 + 20

    =h[3] + 20 + 30 + 20

    =g[0] + 30 + 20 + 30 + 20

    =h[-2] +20 + 30 + 20 + 30 + 20

    = -2 + 120 = 118

    f:{[xF]
     g:{[h;xG] $[xG < 0; xG; h[xG - 2] + 20]}[h]; // doesn't work, as h is not yet defined
     h:{[g;xH] $[xH < 0; xH; g[xH - 3] + 30]}[g];
     g[xF]
     };
    f[10];

    How to make it works?

    vivo replied 8 months, 1 week ago 2 Members · 1 Reply
  • 1 Reply
  • kshepherd

    Member
    July 29, 2022 at 12:00 am
    f:{[xF] 
        g:{[h;xG] $[xG < 0; xG; h[.z.s; xG - 2] + 20]}; 
        h:{[g;xH] $[xH < 0; xH; g[.z.s; xH - 3] + 30]}; 
        g[h; xF] }; 
    f[10];

    Another option is to use .z.s to refer to the calling function when calling the other function.

Log in to reply.