Reputation: 5918
We want to pass complete dictionary as input to a lambda function but by default kdb passes only the values of the dictionary.
How can we get the keys of the dictionary inside lambda function?
We cannot use dictionary name inside lambda because of lexical scoping.
{0N!x}@'(`ab`cd!1 2) / Inside function we get only 1 and 2 and not `ab`cd
One solution is below, but is there any other/better solution:
{key key @'x}(`ab`cd!1 2)
EDIT - Understood the issue, here the issue is not with dictionary passing only values but with "each" function/adverb, as each is passing only the values to the function.
q)key each (`ab`cd!1 2)
ab| ,0
cd| 0 1
Rather if we pass the complete dictionary to the function then we can get keys or values.
q){key x}d
`ab`cd
q){value x}d
1 2
Upvotes: 2
Views: 1469
Reputation: 393
You can pass the whole dictionary in and just key it
{key 0N!x}(`ab`cd!1 2)
Upvotes: 1
Reputation: 13572
If your dictionary values are unique you can pass in the dictionary and apply-each value then within the function you can lookup the corresponding key using ?
like so
q){0N!x?y}[d]@'d:`ab`cd!1 2;
`ab
`cd
Otherwise you'd just have to turn them into pairs:
q){0N!x}@'key[d],'value d:`ab`cd!1 2;
(`ab;1)
(`cd;2)
Upvotes: 2