kdb/q How to use the Do Accumulator using a function that takes in multiple outputs?

77 Views Asked by At

I have a function that takes in 2 tables and outputs the score and state of one table:

// tableOne tableTwo -> Score tableOne
fGetOutput:{[one;two]
    ...
    ?[scoreone >= scoretwo; (scoreone, tableOne); (scoretwo, tableTwo)]}

I also have a function that slightly modifies the contents of a table:

// table -> table
fAddRow:{[table]
    ...
    table}

I want to loop the two of those and get the output at each iteration, so for example, this is what it would look like in pseudocode:

x=0
while x < 10,000:
    output: fGetOutput[tableOne, tableTwo];
    print("Loop # " + x, output[0], output[1]);
    fAddRow;
    x+=1

As this loop runs, I'll get the following output:

Loop #1 Score: 100, Table: ....
Loop #2 Score: 150, Table: ....
Loop #3 Score: 124, Table: ....
...

How do I do the above in q? I know there's the Do Accumulator, but I don't think it allows for the functionality above. Is there another way that I can accomplish getting the output?

1

There are 1 best solutions below

3
On

You could achieve it as an extension of the accumulator \ as per your other question: kdb/Q How to iterate on a function x number of times?

q){`score`t1`t2!$[(c1:count x`t1)<=c2:count x`t2;(2+x`score;(c1+1)#x`t1;x`t2);(3+x`score;x`t1;(c2+1)#x`t2)]}\[10;`score`t1`t2!(50;([]c1:`t1`t1;c2:1 2);([]c1:`t2`t2;c2:3 4))]
score t1                                            t2
-------------------------------------------------------------------------------------------------
50    +`c1`c2!(`t1`t1;1 2)                          +`c1`c2!(`t2`t2;3 4)
52    +`c1`c2!(`t1`t1`t1;1 2 1)                     +`c1`c2!(`t2`t2;3 4)
55    +`c1`c2!(`t1`t1`t1;1 2 1)                     +`c1`c2!(`t2`t2`t2;3 4 3)
57    +`c1`c2!(`t1`t1`t1`t1;1 2 1 1)                +`c1`c2!(`t2`t2`t2;3 4 3)
60    +`c1`c2!(`t1`t1`t1`t1;1 2 1 1)                +`c1`c2!(`t2`t2`t2`t2;3 4 3 3)
62    +`c1`c2!(`t1`t1`t1`t1`t1;1 2 1 1 1)           +`c1`c2!(`t2`t2`t2`t2;3 4 3 3)
65    +`c1`c2!(`t1`t1`t1`t1`t1;1 2 1 1 1)           +`c1`c2!(`t2`t2`t2`t2`t2;3 4 3 3 3)
67    +`c1`c2!(`t1`t1`t1`t1`t1`t1;1 2 1 1 1 1)      +`c1`c2!(`t2`t2`t2`t2`t2;3 4 3 3 3)
70    +`c1`c2!(`t1`t1`t1`t1`t1`t1;1 2 1 1 1 1)      +`c1`c2!(`t2`t2`t2`t2`t2`t2;3 4 3 3 3 3)
72    +`c1`c2!(`t1`t1`t1`t1`t1`t1`t1;1 2 1 1 1 1 1) +`c1`c2!(`t2`t2`t2`t2`t2`t2;3 4 3 3 3 3)
75    +`c1`c2!(`t1`t1`t1`t1`t1`t1`t1;1 2 1 1 1 1 1) +`c1`c2!(`t2`t2`t2`t2`t2`t2`t2;3 4 3 3 3 3 3)

But note that this would get very expensive and memory-hungry if you need to retain the outputs at every step.