【问题标题】:run function recursively over table在表上递归运行函数
【发布时间】:2021-01-24 01:48:29
【问题描述】:

根据表t 中的一些数据和整数pq,对函数fx 的每次调用都会向x 左连接一个新列。
我在下面的 pq 的值上显示了所需的迭代,但不知道如何使用 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?

最后一条语句在 pq 上有两个 for 循环,分别来自 1to2 和 3to4。我确信有更好的方法可以使用扫描/覆盖来实现这一点,但我无法弄清楚。有人可以在这里帮忙吗。

【问题讨论】:

    标签: iterator simulation kdb


    【解决方案1】:

    我们可以将t 作为投影传递,因为它在每次迭代中保持不变,然后对其他变量使用扫描迭代

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

    【讨论】:

    • 谢谢@MarkKelly。但是,fx[;t]/[x;1 2 1 2;3 3 4 4] 中的 ... ;1 2 1 2; 3 3 4 4] 仍在手动编写排列。是否有可能将其替换为 (1+til 2) cross (3+til 2) 之类的内容以使其更通用?
    • 您可以使用点运算符 . 将两个列表作为单独的参数应用 fx[;t]/[x] . flip (1+til 2) cross (3+til 2) 或者您可以将排列设置为变量并传入第一个和最后一个,例如 l:flip (1+til 2) cross (3+til 2); fx[;t]/[x;l 0;l 1]
    猜你喜欢
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多