How to find the rank of a kdb function and throw error

How to find the rank of a kdb function through program for test validation.

For example  -> func: {x+y*10} / rank 2

For func function the rank is 2 however how to identify if the rank is low i.e < 2 or >2 and throw error through program for validation check?

Please suggest how to solve this problem.

q)func: {x+y*10}

//this works with rank 2
q)func [2;4]  
42
//the result is projection
q)func [2;]
{x+y*10}[2;]

//this gives rank error
q)func [2;3;4]
'rank
  [0]  func [2;3;4]

Use value

https://code.kx.com/q/ref/value/#lambda

q)func: {x+y*10}
q)testfunc:{[f;a] na:count value[f][1];;$[na~c:count a;f . a;na<c;'"Too many args";'"Too few args"]}
q)testfunc[func;(1)]
'Too few args
  [0]  testfunc[func;(1)]
       ^
q)testfunc[func;(1;2)]
21
q)testfunc[func;(1;2;3)]
'Too many args
  [0]  testfunc[func;(1;2;3)]
       ^

For lambdas you may be able to use value:

q)f:{x,y,z}
q)count value[f]1
3
q)f:{[a;b]}
q)count value[f]1
2

Although a general answer is more complicated when dynamic loaded functions and projections are possibilities.

This can be even more complicated for functions that are variadic, like ? and enlist. Adverbs can also create variadic functions.

Leave a Comment