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 Re: apply the function, pass the return value to the function again, multiple rounds

  • 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