Search code examples
kdb+

Explain where in KDB applied to vector/dict of integers


I don't understand what where does when applied to a list of integers (or a dict with int values):

where til 4
/ 1 2 2 3 3 3
where `a`b`c`d!til 4
/ `b `c `c `d `d `d

The pattern of the returned value is the same, but I'm not sure what the operation being applied is. I finally get the whole thing about 42 in Hitchhiker's guide to the galaxy... What question does "where" answer in this use case?


Solution

  • For each item of your list x, where takes n copies of index i, where n is a positive integer. so, in your example

    q)til 4
    0 1 2 3
    q)where til 4
    1 2 2 3 3 3
    

    it takes 0 times the index 0, (therefore not shown), 1 times the index 1, 2 times the index 2 and finally 3 times the index 3.

    This works with any sequence

    q)where 3 4 5 2 1 5
    0 0 0 1 1 1 1 2 2 2 2 2 3 3 4 5 5 5 5 5
    

    This is why where works perfectly with boolean masks and returns the index of all true indices

    q)where 01001110010b
    1 4 5 6 9
    

    0 times index 0, 1 time index 1, 0 times index 2 and so on. Now, dictionaries, in KDB/Q dictionaries as well as tables (a tables is simply a flipped column dictionary) are first class citizen, meaning that all operators will work on their value part (a dictionary is a key(index)-value mapping)

    q)show d:`a`b`c`d!til 4
    a| 0
    b| 1
    c| 2
    d| 3
    q)where d
    `b`c`c`d`d`d
    

    where does now take 0 times the index or key a, 1 time the index or key, b, 2 times the index c and 3 times the index d.

    I hope this helps, let me know if it's still unclear.