在KDB中如何通过多个内部函数传递变量?

huangapple go评论68阅读模式
英文:

How do I pass a variable through multiple inner functions in KDB?

问题

我有一个名为 peg 的函数:

  peg: {{$[abs[log x%y]>0.06;y;x]} scan x} 

0.06 是一个我想要从外部传递给 peg 的参数,所以我可以像这样做:0.06 peg x

如何干净地通过这个变量传递?

英文:

I've got a function peg:

  peg: {{$[abs[log x%y]>0.06;y;x]} scan x} 

The 0.06 is a parameter that I'd like to pass to peg from the outside, so I can do something like 0.06 peg x.

What's a clean way of passing this variable through?

答案1

得分: 2

你可以重写函数,添加显式变量,这样你可以保持本地变量名为 xy。下面我添加了一个新的参数 v,用于传递你想要的值。

peg: {[v;x]{[v;x;y]$[abs[log x%y]>v;y;x]}[v] scan x}

peg[0.06;x]

我认为没有其他简单的方法来做到这一点。

英文:

You could rewrite the function to add explicit variables so you can keep the local variable names as x and y. Below I've added a new argument v for the value you want to pass.

peg: {[v;x]{[v;x;y]$[abs[log x%y]>v;y;x]}[v] scan x}

peg[0.06;x]

I don't think there's any other easy way to do this.

答案2

得分: 1

只有内置运算符和关键字可以使用中缀表示法:

x func y

用户定义的函数必须使用括号表示法:

func[x;y]

您可以使用投影来传递参数。kdb 将自动检测隐式参数 [x;y;z]

peg:{{$[abs[log y%z]>x;z;y]}[x] scan y}

为了可读性,建议选择您自己的名称:

peg:{[lim;lis] 
      {[lim;pre;cur] 
        $[abs[log pre%cur]>lim;cur;pre]}[lim] scan lis
     }
英文:

Only inbuilt operators and keywords can use infix notation:

x func y

User defined functions must use bracket notation

func[x;y]

You can use projection to pass in arguments.
kdb will autodetect implicit arguments [x;y;z]

peg:{{$[abs[log y%z]>x;z;y]}[x] scan y}

For readability would suggest to choose your own names though:

peg:{[lim;lis] 
      {[lim;pre;cur] 
        $[abs[log pre%cur]>lim;cur;pre]}[lim] scan lis
     }

答案3

得分: 1

将其通过添加一个额外的变量传递给你的内部函数,就像Thomas所示,并将该变量传递给内部函数。

要回答你关于调用它0.06 peg x的第二部分...你真的只能在内置函数中使用这个“中缀”表示法,尽管你可以通过在q命名空间中定义你的函数来绕过这个问题。

不建议操纵.q命名空间,所以不值得麻烦。

英文:

You pass it through by adding an extra variable to your inner function like Thomas showed, and passing the variable into that.

To answer your second part of calling it 0.06 peg x ... you can really only use this "infix" notation for built-in functions although you can hack it by defining your function in the q namespace

q).q.peg:{{$[abs[log y%z]>x;z;y]}[x]scan y}
q)0.6 peg 1 2 3 4
1 2 2 4

Not recommended to mess with the .q namespace though so not worth the hassle

答案4

得分: 0

在相同的线路上:

peg:{{$[abs[log x%y]>z;y;x]}[;;y]\[x]}

所以如果你定义如下:

v:(1;1.05;1.03;1.1;1;1.04)

运行 peg[v;0.06] 你会得到:

1 1 1 1.1 1 1

英文:

Along the same lines:

peg:{{$[abs[log x%y]>z;y;x]}[;;y]\[x]}

So if you define like:

v:(1;1.05;1.03;1.1;1;1.04)

Running peg[v;0.06] you'll get:

1 1 1 1.1 1 1

huangapple
  • 本文由 发表于 2023年5月22日 22:04:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定