【问题标题】:About xkey implementation关于xkey实现
【发布时间】:2020-09-04 09:51:42
【问题描述】:

xkey 中看到的函数式应用方法.[0!y;();xcols x] 有什么区别:

xkey
k){(#x)!.[0!y;();xcols x]}

还有一个简单的函数调用:x xcols 0!y

为什么xkey中的函数apply首选?

第二个问题是关于从k) 上下文内部调用xcols - 它是如何工作的?由于某种原因,我不能这样做:

t:([]a:`a`s`d;b:1 2 3;c:4 5 6)
k).[0!t;();xcols `a`b]
ERROR: 'xcols 
(attempt to use variable xcols without defining/assigning first (or user-defined signal))

【问题讨论】:

  • 当你使用 k) 时,你不再在 .q 命名空间中,所以你应该使用 .q.xcols 而不是 xcols

标签: kdb k


【解决方案1】:

我认为功能应用可能适用于按名称传递表格时

t:([]a:`a`s`d;b:1 2 3;c:4 5 6)

.[0!t;();xcols[`a`b]]  /this works

`a`b xcols 0!t         /equivalent non functional form works

q)`a`b xcols 0!`t      /this fails when passing `t
'type
  [0]  `a`b xcols 0!`t


q).[0!`t;();xcols[`a`b]]  /though functional form still works with `t


q)`a`b xkey `t            /hence you can use xkey in place
`t

编辑: 为了在完整的答案中包含我的评论,当在 k 中时,您应该在 q 命令前加上 .q 来访问它们,因为这是它们所在的命名空间

q)xcols[`a`b]
k){(x,f@&~(f:cols y)in x)#y}[`a`b]
q)\
  .q.xcols`a`b
k){(x,f@&~(f:cols y)in x)#y}[`a`b]

编辑 2: 为了解决您的评论,您示例的 xcols 归结为

`a`b`c#t

`a`b`c#`t 

不起作用。我没有太多解释为什么它不起作用,除了它是如何设计的。函数形式告诉 q 通过应用 take 函数来修改表。但是上面的示例不是将表重新定义为列的重新排序的公认语法。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多