2022.05.11 06:33 AM
Hi,
I was wondering if there's a way to run a function over x number of iterations and also update a second value based on the function's output.
For e.g.
summer:{[ID;y;z] y:y+z; ID:ID+1}
{summer[x;y;z]}[;2;2]/[{x<750};0]
Currently this runs for 750 iterations however y and z are set to 2 on input so y always equals 4, I have checked this using namespaces.
Is there a way to include another over somewhere so that y will run like 2+2=4, 4+2=6... for 750 iterations?
Any help appreciated!!
PS. The code is being used within peach, that's why I can't just use namespaces or a global variable to upsert into. 🙂
2022.05.11 08:16 AM
Something more like:
q)750{(x[0]+1;x[1]+x[2];x[2])}/0 0 2
750 1500 2
Or use a dictionary for readability:
q)750{x[`ID]+:1;x[`y]:sum x`y`z;x}/`ID`y`z!0 0 2
ID| 750
y | 1500
z | 2
2022.05.11 07:23 AM
Is the /
for of do
what you are looking for?
https://code.kx.com/q/ref/accumulators/#do
q)750 +/2 2
754
2022.05.11 07:48 AM - edited 2022.05.11 07:49 AM
Hey, thanks for the reply.
So I think the above is quite close however it only allows you run sum, not appending to iteration id as required.
Following similar to above:
q)summer1:{[x;y] .m.x:x; x:x+y}
q)750 summer1[;2]/2;
q).m.x
1500
I'm probably missing something obvious but is there a way to manipulate this to append to ID per iteration so that:
q)summer:{[ID;y;z] y:y+z;.m.id:.m.id,ID;.m.y:y; ID:ID+1}
q).m.y
1500 (as above)
q).m.ID
750 (missing piece)
Thanks again!
2022.05.11 08:16 AM
Something more like:
q)750{(x[0]+1;x[1]+x[2];x[2])}/0 0 2
750 1500 2
Or use a dictionary for readability:
q)750{x[`ID]+:1;x[`y]:sum x`y`z;x}/`ID`y`z!0 0 2
ID| 750
y | 1500
z | 2
2022.05.11 08:22 AM
The very one. 🙂
Fair play and thanks!
2022.05.12 04:03 AM
See article on the accumulator iteration operators:
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.