cancel
Showing results for 
Search instead for 
Did you mean: 

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

Matt_Fang
New Contributor

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.

1 ACCEPTED SOLUTION

rocuinneagain
Valued Contributor
Valued Contributor

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

 

 

 

View solution in original post

1 REPLY 1

rocuinneagain
Valued Contributor
Valued Contributor

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