Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of available functions and their parameters in KDB/Q?

Tags:

kdb

How would I go about getting a list of available functions and their parameters in a given namespace?

like image 760
J.P. Armstrong Avatar asked Oct 04 '13 19:10

J.P. Armstrong


2 Answers

http://code.kx.com/q/ref/syscmds/#f-functions

\f .
\f .namspace

For functions you will have to check parameters individually by just giving the name of function

.n.function

will give you not only the parameters but the whole function definition.

like image 81
Naveen Sharma Avatar answered Sep 20 '22 02:09

Naveen Sharma


this can surely be improved upon, but thought I'd share as a quick way to get the ball rolling. This will retrieve every global user defined function in every workspace and create a dictionary of namespapaces to functions to parameters.

 q)getparams:{k!{n[w]!@'[;1] value each f w:where 100h=type each f:get each ".",/:"." sv/:string x,/:n:y x}[;m]  each key m:k!system each "f .",/:string k:key `}
 q)f1:{x+y+z}
 q).n1.f2:{x*x}
 q).n1.a:2
 q).n2.f3:{y+y}
 q)show r:getparams[]
 q | `aj`aj0`asc`asof`avgs`cols`cor`cov`cross`cut`desc`dev`each`ej`except`fby`..
 Q | `Cf`IN`L`S`V`addmonths`bv`chk`cn`d0`dd`def`dpft`dpt`dsftg`dt`en`f`fc`ff`f..
 h | `cd`code`data`eb`ec`ed`edsn`es`fram`ha`hb`hc`hn`hr`ht`hta`htac`htc`html`h..
 n1| (,`f2)!,,`x
 n2| (,`f3)!,`x`y
q)r[`n1;`f2]
,`x

[EDIT] the original function was wrong. It missed the global namespace (`) and didn't capture composition, or functions defined with an adverb. The below corrects this, but seems overly convoluted. I'll still leave it here though in case anyone wants to post a nicer solution (so that I too can learn from that)

  getparams:{k!{n[w][w2]!@'[;1] v w2:where 0h=type each v:value/[{type[x] in y}[;t]; ] each f:f w:where in[ ;(t:"h"$100,105+til 7)] type each f:get each `$".",/:"." sv/:string x,/:n:y x}[;m]  each key m:k!system each "f .",/:string k:`,key `}
like image 42
JPC Avatar answered Sep 21 '22 02:09

JPC