KX Community

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

Home Forums kdb+ apply the function, pass the return value to the function again, multiple rounds

  • apply the function, pass the return value to the function again, multiple rounds

    Posted by matt_fang on April 16, 2022 at 12:00 am

    I have a list of symbols, say `A`B`C. I have a table tab0; A function that takes in a table plus a string as arguments.

    tab1: f[tab0;`A] tab2: f[tab1;`B] tab3: f[tab2;`C] 

    I only care about the final values. But my list of symbols can be long and can have variable length, so I don’t want to hardcode above. How do I achieve it?

    I think it has something to do with https://code.kx.com/q/ref/accumulators/ but I really struggle to figure out the syntax.

    matt_fang replied 8 months ago 2 Members · 1 Reply
  • 1 Reply
  • rocuinneagain

    Member
    April 17, 2022 at 12:00 am

    You can so this using over

    q)tab:([] A:1 2 3;B:4 5 6;C:7 8 9) 
    // Sample table 
    
    q)tab A B C ----- 1 4 7 2 5 8 3 6 9
    q)func:{![x;();0b;enlist[y]!enlist (+;y;1)]} //Function I want to run
    q)func[tab;`A] // Same as: update A:A+1 from tab A B C ----- 2 4 7 3 5 8 4 6 9
    q)func over enlist[tab],`A`B`C // Using over accumulator A B C ------ 2 5 8 3 6 9 4 7 10

     

     

     

Log in to reply.