在扩展代码时,我发现自己需要向函数发送越来越多的数据,以便正确处理和计算预测。
我现在正处于一个分叉点,要继续,我需要将几个列值连接为一个大字符串或符号(我很有信心做到这一点,但维护起来很痛苦(,而不是创建一个字典(我认为这是最佳实践(来传递给我的函数。
例如,我有一个包含数据的表,它使用输入作为函数变量来计算并返回数据的时间数组(最终被取消分组(
使用下面的例子,使用一个名为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;
有人有一个很好的例子来说明如何创建一个字典,并将其传递给基于大量现有表列的函数吗?
要从上一个问题中使用的示例开始工作,如果您想采用下表
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])};
然后,您可以在仅由列aa
、bb
和cc
组成的中间表上使用each
,而不是分别使用和('
(
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
这可能是一个愚蠢的问题,但您是否尝试过在带有函数的表上使用each
?
表只是字典的列表,当你对它们进行索引时,你就会得到当时的字典。例如:
q)t: ([] a: 5 ? 10; b: 5 ? 10)
q)t 0
a| 1
b| 4
因此,如果我们有一个有九列的表
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
这对你有用吗?还是我有点误解了这个问题?