Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exists function in q/kdb

Tags:

kdb

I need to write a function in q/kdb which takes a variable v and returns 1b if v is defined and 0b if it is not:

$ a:2
$ doesExist`a
1b
$ doesExist`b
0b

Any ideas appreciated.

like image 973
Viriya Avatar asked Jul 23 '14 23:07

Viriya


3 Answers

q)doesExist:{x~key x}
q)a:2
q)doesExist`a
    1b
q)doesExist`b
    0b
like image 118
MdSalih Avatar answered Nov 17 '22 17:11

MdSalih


key`.

Will give you all the variables in the current namespace.

Similarly

key`.foo

Will give you all the variables in the .foo namespace.

By extension:

`a in key`.

Will give you the boolean you're after

like image 7
Manish Patel Avatar answered Nov 17 '22 17:11

Manish Patel


Based on MdSalih's answer and pamphlet's comment, perhaps we can test the opposite. Since key outputs an empty list if the variable is not defined, we should test for that, which gets us around the keyed table problem.

q)AnswerToLifeUniverseAndEverything:42
q)doesExist:{not () ~ key x}
q)doesExist[`AnswerToLifeUniverseAndEverything]
1b
q)doesExist[`UltimateQuestionToLifeUniverseAndEverything]
0b
like image 5
DrCurrency Avatar answered Nov 17 '22 15:11

DrCurrency