代码之家  ›  专栏  ›  技术社区  ›  Gerry

在表上递归运行函数

  •  0
  • Gerry  · 技术社区  · 5 年前

    每次调用函数 fx left将新列连接到 x 基于表中的一些数据 t 和整数 p q .
    我在以下值上显示了所需的迭代 p q 下面,但不知道如何使用KDB+迭代器正确完成。
    样本数据和解决方案:

    x:([]date:(2013.07.01+til 10);ords:til 10); /Some random table with date (key column)
    t:p:([]date:(2013.07.01+1000#til 200);px:1000?10e); /Some random table.
    fx:{[x;t;p;q]
        /Do something with t;p;q and left-join output column to x on date;
        t:([]date:(2013.07.01+til 10);ords:10?10000);
        col_names:(`date;`$""sv(string(`P);string(p);string(`Q);string(q)));
        t: x lj 1!col_names xcol t;
        :t
        };
    
    /Run simulation for p=1 to 2 and q=3 to 4 as follows:
    fx[ fx[ fx[ fx[x;t;1;3]; t;2;3]; t;1;4]; t;2;4] /How to do this iteration properly?
    

    最后一条语句有两个for循环 p q 分别从1to2和3to4。我确信有一种更好的方法可以通过扫描/翻转来实现这一点,但我无法弄清楚。有人能帮帮我吗。

    0 回复  |  直到 5 年前
        1
  •  2
  •   Mark Kelly    5 年前

    我们可以通过 t 作为投影,因为它在每次迭代中保持不变,然后对其他变量进行扫描迭代

    fx[;t;;]/[x;1 2 1 2;3 3 4 4]