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

3

6

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

Mccarver answered 4/10, 2013 at 19:51 Comment(0)
P
10

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.

Pantechnicon answered 5/10, 2013 at 4:56 Comment(1)
I figured out that get could get me the parameters for a given function. code.kx.com/wiki/Reference/getMccarver
V
4

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 `}
Valkyrie answered 11/10, 2013 at 13:9 Comment(1)
I realize this an old thread but is there a way to reference just the first, blank name space i.e. I just want to build a dictionary of my custom functions vs. the native q functions? Thanks in advance, a very helpful thread!Colorific
U
2

I addition to Naveen's answer, you can call value functionName which will give you a list of items, e.g. parameter names and the compiled byte code

Univalent answered 6/10, 2013 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.