How to find the valence of a kdb function

Viewed 103

Is there a way to find the valence (rank) of a function in q?

ie I'm looking for a function v such that

v[{42}] gives 0
v[{x*x}] gives 1
v[{x+y}] gives 2

etc

1 Answers

What you want to do is

q)f:{x+y+z}
q)/the second item returned by value f is the args of f, so we simply count them.
q)value f
0x63624161410003
`x`y`z  <--- args of f
`symbol$()
,`
7 5 6 3 4 2 2
"..f"
""
-1
"{x+y+z}"
q)count value[f]1
3

q)/ wrap it in a function
q)v:{count value[x]1}

(although should be noted that a niladic func like {42} will still show 1 param, x) see below: https://code.kx.com/q/ref/value/#lambda

Related