kdb/q 如何使用接受多个输出的函数来使用 Do 累加器?

问题描述 投票:0回答:1

我有一个函数,它接受 2 个表并输出一个表的分数和状态:

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

我还有一个稍微修改表格内容的函数:

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

我想循环其中两个并在每次迭代时获取输出,因此,例如,这就是伪代码中的样子:

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

当这个循环运行时,我将得到以下输出:

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

如何在 q 中执行上述操作?我知道有 Do Accumulator,但我认为它不允许实现上述功能。还有其他方法可以实现获取输出吗?

loops kdb accumulate accumulator
1个回答
0
投票

您可以将其作为累加器的扩展来实现

\
根据您的其他问题:kdb/Q 如何迭代函数 x 次?

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)

但请注意,如果您需要在每一步都保留输出,这将变得非常昂贵且占用内存。

© www.soinside.com 2019 - 2024. All rights reserved.