KDB“值”范围作为局部变量

KDB“值”范围作为局部变量,kdb,Kdb,在函数能够识别其局部变量的情况下,是否可以将字符串作为命令传递给函数? 例如,在以下情况下,值a+:1;无法识别函数中的局部变量 .th.f:{[a] iterations: (1+ til 4); while[count (iterations); value "a+:1"; iterations: 1_iterations a}; test: .th.f[5]; test //gives 5 instead of 10 ...[1 + 2 + 3

在函数能够识别其局部变量的情况下,是否可以将字符串作为命令传递给函数? 例如,在以下情况下,值a+:1;无法识别函数中的局部变量

.th.f:{[a]
   iterations: (1+ til 4);
   while[count (iterations);
      value "a+:1";
      iterations: 1_iterations
   a};

test: .th.f[5];
test //gives 5 instead of 10 ...[1 + 2 + 3 + 4]
我需要的是值a+:1;能够知道a实际上是函数内部的局部变量

.th.f:{[a]
   iterations: (1+ til 4);
   while[count (iterations);
      value "a+:1";
      iterations: 1_iterations
   a};

test: .th.f[5];
test //gives 5 instead of 10 ...[1 + 2 + 3 + 4]

我目前正在处理一个更复杂的问题,其中将字符串作为q命令传递是唯一的解决方法。

您可以使用join构建字符串,并对参数应用字符串,而不是将参数放在字符串中作为字符处理。所以不是

func:{[a] value "a+1"}
你可以

func:{[a] value string[a],"+1"}

虽然我发现很难接受传递这样的字符串是解决此问题的唯一方法,而且我不建议使用此解决方案……类似的内容可能会对您有所帮助:

local_context:([]());
.th.f:{[a;v;f] n:?[local_context;();();`$v];
    it:1+til 4;
    while[count it;n:f n;it:1_it];
    n};

q).th.f[5;"a";{x+1}]
9
q).th.f[10;"a";{x+1}]
14
q).th.f[10;"a";{x+2}]
18
这里我将a+:1拆分为变量a和函数{x+1}。如果您仍然坚持它必须是a+:1,那么您可以找到一种方法将a+:1转换为变量a和函数{x+1}

该方法还扩展到其他变量:

local_context:([]());
.th.f:{[a;b;v;f] n:?[local_context;();();`$v];
    it:1+til 4;
    while[count it;n:f n;it:1_it];
    n};

q).th.f[5;100;"a";{x+1}]
9
q).th.f[5;100;"b";{x+1}]
104
q).th.f[10;100;"a";{x+1}]
14
q).th.f[10;100;"b";{x+2}]
108

但是,如果您退后一步,就必须有更好的方法来解决您的问题,所以您必须保留代码的a+:1部分,还是这是您的解决方案? 如果它是可变的,但必须是字符串,我会选择:

f:{[a;strFunc]
    itrs:5;
    while[itrs;
        a:value strFunc,string a;
        itrs-:1];
    a};
f[5;"1+"] /Returns 10

我目前正在研究一个更复杂的问题,其中传递字符串作为q命令是唯一可以解决的方法。更复杂的问题是什么?