KDB - 从 table 列创建字典以克服 8 个函数参数限制?
KDB - Creating a dictionary from table columns to overcome the 8 function argument limitation?
随着我扩展代码,我发现自己需要向函数发送越来越多的数据,以便正确处理和计算预测。
我现在正处于一个岔路口,我需要将几个列值连接为一个大字符串或符号(我很有信心我能做到,但看起来很难维护)与创建字典(这我理解是最佳实践)传递给我的函数。
例如,我有一个 table 数据,它使用输入作为函数变量来计算,return 一个时间数据数组(最终被取消分组)
使用下面的示例,该示例使用一个名为 Call_Function 的字段,其中包含函数名称,我想传递几个其他变量(即 tok9、tok10、tok11 等)
//working function with 8 ordered arguments
applyFcast:{[t] update fcast: first[Call_Function]'[tok1;tok2;tok3;tok4;tok5;tok6;tok7;tok8] from t where not null tok1, 0= count each fcast};
updateTable: applyFcast ::; //run function
t: updateTable over t; //converge over table t
t: ungroup t;
我正在考虑创建类似于下面的字典
dictArguments: {[t] exec tok1, tok2, tok3, tok4, tok5, tok6, tok7, tok8, tok9, tok10, tok11 from t};
applyFcast:{[t] update fcast: first[Call_Function]'[dictArguments] from t where not null dictArguments[tok1], 0= count each fcast};
updateTable: applyFcast dictArguments ::; //run functions in order
t: updateTable over t; //converge over table t
t: ungroup t;
有没有人有一个很好的例子来说明如何创建一个字典来传递给基于大量现有 table 列的函数?
可能是个愚蠢的问题,但是您是否尝试过在 table 上使用带有函数的 each
?
表只是字典的列表,当你对它们进行索引时,你就在那个时候得到了字典。例如:
q)t: ([] a: 5 ? 10; b: 5 ? 10)
q)t 0
a| 1
b| 4
因此,如果我们有一个包含九列的 table
q)t: ([] c1:5?10; c2:5?10; c3:5?10; c4:5?10; c5:5?10; c6:5?10; c7:5?10; c8:5?10; c9:5?10)
q)t
c1 c2 c3 c4 c5 c6 c7 c8 c9
--------------------------
9 5 9 8 4 3 5 5 8
7 2 0 5 2 7 6 4 1
6 3 0 2 8 8 4 2 7
6 9 0 8 0 2 1 7 2
4 5 9 6 5 1 3 8 4
和一个函数 f
你想对其进行操作,你可以执行以下操作:
q)f: {(x `c1) + x `c9}
q)f each t
17 8 13 8 8
这对你有用吗,还是我有点误解了这个问题?
如果您想采用 table 以下 table
从您在上一个问题中使用的示例开始工作
q)show t:flip `id`seg`aa`bb`cc`Uknown`Call_Function!(`AAA`AAA`AAA`BBB`CCC;1 2 3 1 1;1500 0n 400 40 900;0n 200 30 40 0n;0.4 0.25 0n 0n 0.35;`bb`aa`cc`cc`bb;`Solvebb`Solveaa`Solvecc`Solvecc`Solvebb);
id seg aa bb cc Uknown Call_Function
------------------------------------------
AAA 1 1500 0.4 bb Solvebb
AAA 2 200 0.25 aa Solveaa
AAA 3 400 30 cc Solvecc
BBB 1 40 40 cc Solvecc
CCC 1 900 0.35 bb Solvebb
并通过将 aa
、bb
和 cc
变量作为字典切片而不是作为三个单独的参数传递来应用您的 Call_Function
,然后您可以将您的 Call_Function
定义为
q)Solvebb:{[d](d[`aa]%d[`cc])*(1-exp(neg d[`cc]*1+til 5))};
q)Solveaa:{[d](d[`bb]+d[`cc];d[`bb]*d[`cc])};
q)Solvecc:{[d](d[`aa]+d[`bb];d[`aa]*d[`bb])};
然后,您可以在仅包含列的中间 table 上使用 each
,而不是使用 each both ('
) aa
、bb
和 cc
q)ungroup update result:first[Call_Function] each ([]aa;bb;cc) by Call_Function from t
id seg aa bb cc Uknown Call_Function result
---------------------------------------------------
AAA 1 1500 0.4 bb Solvebb 1236.3
AAA 1 1500 0.4 bb Solvebb 2065.016
AAA 1 1500 0.4 bb Solvebb 2620.522
AAA 1 1500 0.4 bb Solvebb 2992.888
AAA 1 1500 0.4 bb Solvebb 3242.493
AAA 2 200 0.25 aa Solveaa 200.25
AAA 2 200 0.25 aa Solveaa 50
AAA 3 400 30 cc Solvecc 430
AAA 3 400 30 cc Solvecc 12000
BBB 1 40 40 cc Solvecc 80
BBB 1 40 40 cc Solvecc 1600
CCC 1 900 0.35 bb Solvebb 759.3735
CCC 1 900 0.35 bb Solvebb 1294.495
CCC 1 900 0.35 bb Solvebb 1671.589
CCC 1 900 0.35 bb Solvebb 1937.322
CCC 1 900 0.35 bb Solvebb 2124.581
随着我扩展代码,我发现自己需要向函数发送越来越多的数据,以便正确处理和计算预测。
我现在正处于一个岔路口,我需要将几个列值连接为一个大字符串或符号(我很有信心我能做到,但看起来很难维护)与创建字典(这我理解是最佳实践)传递给我的函数。
例如,我有一个 table 数据,它使用输入作为函数变量来计算,return 一个时间数据数组(最终被取消分组)
使用下面的示例,该示例使用一个名为 Call_Function 的字段,其中包含函数名称,我想传递几个其他变量(即 tok9、tok10、tok11 等)
//working function with 8 ordered arguments
applyFcast:{[t] update fcast: first[Call_Function]'[tok1;tok2;tok3;tok4;tok5;tok6;tok7;tok8] from t where not null tok1, 0= count each fcast};
updateTable: applyFcast ::; //run function
t: updateTable over t; //converge over table t
t: ungroup t;
我正在考虑创建类似于下面的字典
dictArguments: {[t] exec tok1, tok2, tok3, tok4, tok5, tok6, tok7, tok8, tok9, tok10, tok11 from t};
applyFcast:{[t] update fcast: first[Call_Function]'[dictArguments] from t where not null dictArguments[tok1], 0= count each fcast};
updateTable: applyFcast dictArguments ::; //run functions in order
t: updateTable over t; //converge over table t
t: ungroup t;
有没有人有一个很好的例子来说明如何创建一个字典来传递给基于大量现有 table 列的函数?
可能是个愚蠢的问题,但是您是否尝试过在 table 上使用带有函数的 each
?
表只是字典的列表,当你对它们进行索引时,你就在那个时候得到了字典。例如:
q)t: ([] a: 5 ? 10; b: 5 ? 10)
q)t 0
a| 1
b| 4
因此,如果我们有一个包含九列的 table
q)t: ([] c1:5?10; c2:5?10; c3:5?10; c4:5?10; c5:5?10; c6:5?10; c7:5?10; c8:5?10; c9:5?10)
q)t
c1 c2 c3 c4 c5 c6 c7 c8 c9
--------------------------
9 5 9 8 4 3 5 5 8
7 2 0 5 2 7 6 4 1
6 3 0 2 8 8 4 2 7
6 9 0 8 0 2 1 7 2
4 5 9 6 5 1 3 8 4
和一个函数 f
你想对其进行操作,你可以执行以下操作:
q)f: {(x `c1) + x `c9}
q)f each t
17 8 13 8 8
这对你有用吗,还是我有点误解了这个问题?
如果您想采用 table 以下 table
从您在上一个问题中使用的示例开始工作q)show t:flip `id`seg`aa`bb`cc`Uknown`Call_Function!(`AAA`AAA`AAA`BBB`CCC;1 2 3 1 1;1500 0n 400 40 900;0n 200 30 40 0n;0.4 0.25 0n 0n 0.35;`bb`aa`cc`cc`bb;`Solvebb`Solveaa`Solvecc`Solvecc`Solvebb);
id seg aa bb cc Uknown Call_Function
------------------------------------------
AAA 1 1500 0.4 bb Solvebb
AAA 2 200 0.25 aa Solveaa
AAA 3 400 30 cc Solvecc
BBB 1 40 40 cc Solvecc
CCC 1 900 0.35 bb Solvebb
并通过将 aa
、bb
和 cc
变量作为字典切片而不是作为三个单独的参数传递来应用您的 Call_Function
,然后您可以将您的 Call_Function
定义为
q)Solvebb:{[d](d[`aa]%d[`cc])*(1-exp(neg d[`cc]*1+til 5))};
q)Solveaa:{[d](d[`bb]+d[`cc];d[`bb]*d[`cc])};
q)Solvecc:{[d](d[`aa]+d[`bb];d[`aa]*d[`bb])};
然后,您可以在仅包含列的中间 table 上使用 each
,而不是使用 each both ('
) aa
、bb
和 cc
q)ungroup update result:first[Call_Function] each ([]aa;bb;cc) by Call_Function from t
id seg aa bb cc Uknown Call_Function result
---------------------------------------------------
AAA 1 1500 0.4 bb Solvebb 1236.3
AAA 1 1500 0.4 bb Solvebb 2065.016
AAA 1 1500 0.4 bb Solvebb 2620.522
AAA 1 1500 0.4 bb Solvebb 2992.888
AAA 1 1500 0.4 bb Solvebb 3242.493
AAA 2 200 0.25 aa Solveaa 200.25
AAA 2 200 0.25 aa Solveaa 50
AAA 3 400 30 cc Solvecc 430
AAA 3 400 30 cc Solvecc 12000
BBB 1 40 40 cc Solvecc 80
BBB 1 40 40 cc Solvecc 1600
CCC 1 900 0.35 bb Solvebb 759.3735
CCC 1 900 0.35 bb Solvebb 1294.495
CCC 1 900 0.35 bb Solvebb 1671.589
CCC 1 900 0.35 bb Solvebb 1937.322
CCC 1 900 0.35 bb Solvebb 2124.581