KX Community

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

Home Forums kdb+ How to write local function which can be called from different levels

  • How to write local function which can be called from different levels

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

    Hi,

    My code below works without error:

    No Error

    g:`;
    g:{[xG] xG + 2}; // global; works for f and h
    f:{[xF] 
    h:{[xH] g[xH] + 3};
    show g[xF] + h[xF];
    }
    f[40]; // i.e. 87; no error

     

    However, I prefer a local g instead of a local one. I also tried below, but both failed:

    Trial 1 (Failed)

    g:`;
    f:{[xF] 
    g:{[xG] xG + 2}; // local; doesn't work for h
    h:{[xH] g[xH] + 3};
    show g[xF] + h[xF];
    }
    f[40]; // error

    and

    Trial 2 (Failed)

    g:`;
    f:{[xF] 
    h:{[xH] g:{[xG] xG + 2}; g[xH] + 3}; // local; doesn't work for f
    show g[xF] + h[xF];
    }
    f[40];// error

    Is it possible to have a local g without an error? Thanks.

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

    Member
    July 29, 2022 at 12:00 am

    You could pass it in to a projection

     

    q)f:{[xF] g:{[xG] xG + 2};
           h:{[g;xH] g[xH] + 3}[g];
           g[xF] + h[xF]} 
    
    q)f[40] 87

     

    Or composition in this basic case

     

    q)f:{[xF] g:{[xG] xG + 2};
            h:+[3] g @;
            g[xF] + h[xF]} 
    
    q)f[40] 87

     

     

Log in to reply.