When debugging, I want to insert something into a function and see when this function gets called.
0N! is one way to go.
Another way is to use “show”.
The end result of using show is arguably identical to using ON!, but the implementations appear to be different.
Implementation of “show”
q)show
k){1 .Q.s x;}
q).Q.s
k){$[(::)~x;“”;`/:$[10h=@r:@[S[(.“\c”)-2 1;0];x;::];,-3!x;r]]}
Implementation of 0N!
q)0N!
![0N]
I lean towards 0N! to reveal the data and structure.
e.g.
q)0N!enlist s#0 1; ,
s#0 1
q)show enlist `s#0 1;
0 1
If I’m interested in the data only and am confident of the structure, I might use show.
For debugging, my colleague Pierre cooked up the following wrapper a while back
L:{('[{.[x;y;{0N!(string x;y;z);'z}[x;y]]};enlist])}
sample usage:
q)f:L{x+g[y;z]}
q)g:L{x*y}
q)f[1;“a”;2]
(“{x*y}”;(“a”;2);“type”)
(“{x+g[y;z]}”;(1;“a”;2);“type”)
{.[x;y;{0N!(string x;y;z);'z}[x;y]]}
'type
which shows the stack of wrapped functions on error.
A small tweak shows the func+args as they are called
q)L:{('[{0N!(x;y);x . y};enlist])}
q)f:L{x+g[y;z]}
q)g:L{x*y}
q)f[1;2;3]
({x+g[y;z]};1 2 3)
({x*y};2 3)
7
Aaron also cooked up the helpful “what the function”
http://www.q-ist.com/search?q=wtf
Thanks Charles.
On second look, 0N! and show are quite different.
In particular, (from the wiki):
0N! returns the right hand side after printing its unformatted text representation to console. This is useful for debugging, or avoiding formatting which may obscure the data’s structure.
q)2+0N!335
show only writes to the console; it does not return the formatted result:
q)a:show til 50 1 2 3 4q)a / a is null