首页 文章

kdb - 'where'函数如何工作

提问于
浏览
3

我想通过以下声明了解发生的情况:

sum(til 100) where 000011110000b

该行评估为22,我无法弄清楚原因 . sum(til 100)4950 . where 000011110000b 返回列表 4 5 6 7 . kdb参考页面似乎没有解释这个用例 . 为什么以上行评估为22?

另外,为什么下面的行会导致错误

4950 where 000011110000b

1 回答

  • 3

    方括号用于函数调用参数,而不是括号 . 所以上面的解释是:

    sum[(til 100) where 000011110000b]
    

    你现在可以看到为什么它会评估为22,即它是列表 til 100 的第5,第6,第7,第8个值的总和,即(0 ... 99),因为 where 正在索引到 til 100 使用布尔列表 000011110000b

    q)til[100] where 000011110000b
    4 5 6 7
    

    您会注意到,在调用函数时可以省略方括号;但是这样做时你需要确保它将被解析/解释为预期 . 通常,代码从右向左进行求值,因此在这种情况下,首先评估 (til 100) where 000011110000b ,并将结果传递给 sum 函数 .

    关于 4950 where 000011110000b - 再次如果我们从右到左思考,解释器试图将 where 000011110000b 的结果传递给函数 4590 . 虽然 4590 isn 't a named function in kdb - this is the format used for IPC, i.e. executing commands on other kdb processes over TCP. So you are telling kdb to use the OS file descriptor number 4590 (which almost certainly won' t对应于有效的TCP连接)但运行命令 where 000011110000b . 有关详细信息,请参阅此处的"message formats"部分:http://code.kx.com/q/tutorials/startingq/ipc/

相关问题