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

StackOverflow https://stackoverflow.com/questions/19189621

  •  30-06-2022
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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 `}

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top