kdb+k

Applying dictionary to dictionary


Recently I've found a technique of applying dict to dict. It is something like this:

(3 4 5!6 7 8)[(`a`b)!(2 3)] ~ (`a`b!0N 6)

or even this, which looks more natural for reading from left to right:

(@[;(`a`b)!(2 3)](3 4 5!6 7 8)) ~ (`a`b!0N 6)

Can I use this behavior (are there any caveats)? Is it described somewhere in the official docs?


It seems like absolutely mind-blowing technique: we definitely apply function(list/dictionary/table) to it's argument, not just pass argument to a function.


Solution

  • I think it's reasonable if you view a dictionary as behaving like a function which takes keys as input and outputs values.

    So in the same way that a function that doubles would double the values of your input while retaining the keys of your input:

    q)f1:2*
    q)f1[`a`b!2 3]
    a| 4
    b| 6
    

    a "function" (dictionary) that takes keys and outputs values would do similar:

    q)f2:3 4 5!6 7 8
    q)f2[`a`b!2 3]
    a|
    b| 6