Getting _all_ workspace names (incl. nested) - in pursuit of ellegance

Hi!

I’m putting together a little helper for q developers/users (hope to
present solid beta for community sometime in January). One of its
features will be - server workspace explorer.

Tricky part that I ran into just now - is to get names of all
workspaces, including nested ones. (key` will give you only ones from
root)

So far I came up with those two lines for the task:
f:{,/[except[x,{$[99=@:[.:];$[(@[;0]!:[.:];@[;0].:[.:
])~(;::);f[x];];]}each sv[;]each x,:/:1_key .:;::]]};
raze f each sv[;]each,',key

But there’s a couple of problems I’m having with this solution:

  1. Temporary lambda (with some more rare name than f) should be
    created and deleted afterwards.
  • Shouldn’t there be away to do inline recursion?
  1. I just can’t help thinking that there should be a simpler way :).

Since I acquainted with q for not that long yet, I wanted to ask
advice from real gurus out there ;-).

Thank you!
_Oz_

and if only want to return namespaces,

q)f:{$[(x~r:key x);[:x];[t:{` sv x,y}[x;] each r;:(,/)x,f each t where {99h~type value x} each t]]}

q)f `

On Dec 11, 2010, at 3:34 AM, _Oz_ wrote:

> I’m putting together a little helper for q developers/users (hope to
> present solid beta for community sometime in January). One of its
> features will be - server workspace explorer.
>
> Tricky part that I ran into just now - is to get names of all
> workspaces, including nested ones. (key will give you only ones from \> root) \> \> So far I came up with those two lines for the task: \> f:{,/[except[x,{$[99=@:[.:[x]];$[(@[;0]!:[.:[x]];@[;0].:[.: \> [x]])~(;::);f;];]}each sv[;]each x,:/:1\_key .:[x];::]]}; \> raze f each sv[;]each,',key`
>
> But there’s a couple of problems I’m having with this solution:
> 1) Temporary lambda (with some more rare name than f) should be
> created and deleted afterwards.
> - Shouldn’t there be away to do inline recursion?
> 2) I just can’t help thinking that there should be a simpler way :).
>
> Since I acquainted with q for not that long yet, I wanted to ask
> advice from real gurus out there ;-).

here’s a few things i’ve been tweaking for the past couple years that
might help.

wtfcat gets the fully-qualified names of everything in or below its
argument (which is defaulted to do both . and key, i.e. everything)

wtffd gets specifically all the lambdas in the workspace, then
recursively searches them for embedded lambdas and gives those
unambiguous names

wtf returns the name of a function matching its argument (intended use
is “wtf .z.s” from an error prompt)

callers is still a little buggy, but is meant to identify where a
function is called

(recursion in general is done with .z.s
https://code.kx.com/trac/wiki/Reference/dotzdots)

mapd:{y!x y,:()}

dfilter:{where[x y]#y,:()}

wtfcat:{$[$[null x;1;99=type get x;~first key x;0b]|.~x;
(raze/).z.s each’x .Q.dd’'key each x:$[null x;.;x];x]}

wtffd:{(!). flip raze
{enlist[(x;y)],raze .z.s’[(x,`lambda).Q.dd/:til count p;p@:where
100=type each p:get y]}.’
{key,'get x}dfilter[100=type each]mapd[get each]wtfcat x}

wtf:{wtffd?x}

callers:{where x{x in get[y]3}'wtffd}

Aaron Davies
aaron.davies@gmail.com

Thank you so much! :)Between Aaron and Pawan suggestions and my initial approach I was ableto create query that’s returning all namespaces inline (without needto create temporary functions and cleanup after):{$[x~r:!:;:x;[t:{ sv x,y}[x;] each r;(,/)x,.z.s each t(&:){(@[;0]!:[.:[x]];@[;0].:[.:[x]])~(;::)}each t:t(&:){99h~@:[.:]}each t]]}Sorry, pawan, had to add check for first element to be ((),)!((),::),can’t treat all dictionaries as namespaces.Great approach with the step of recursion - streamlining my ill-born1.5 steps into one with a clean branch.Thank you for .z.s hint and sharing your toolbox, Aaron, will be veryuseful in checking my C++ client-side referencing engine against.Great idea with figuring out callers, I was focused mostly ondependencies, will shift my focus a bit in this direction.As soon as QPad will be ready for the audience, will post linkhere :).Thank you!On Dec 11, 6:21?pm, Aaron Davies <aaron.dav…> wrote:> On Dec 11, 2010, at 3:34 AM, Oz wrote:>>>>>>>>>> > I’m putting together a little helper for q developers/users (hope to> > present solid beta for community sometime in January). One of its> > features will be - server workspace explorer.>> > Tricky part that I ran into just now - is to get names of all> > workspaces, including nested ones. (key will give you only ones from&gt; &gt; root)&gt;&gt; &gt; So far I came up with those two lines for the task:&gt; &gt; f:{,/[except[x,{$[99=@:[.:[x]];$[(@[;0]!:[.:[x]];@[;0].:[.:&gt; &gt; [x]])~(;::);f;];]}each sv[;]each x,\:/:1_key .:[x];::]]};&gt; &gt; raze f each sv[;]each,',key&gt;&gt; &gt; But there's a couple of problems I'm having with this solution:&gt; &gt; 1) Temporary lambda (with some more rare name than f) should be&gt; &gt; created and deleted afterwards.&gt; &gt; - Shouldn't there be away to do inline recursion?&gt; &gt; 2) I just can't help thinking that there should be a simpler way :).&gt;&gt; &gt; Since I acquainted with q for not that long yet, I wanted to ask&gt; &gt; advice from real gurus out there ;-).&gt;&gt; here's a few things i've been tweaking for the past couple years that might help.&gt;&gt; wtfcat gets the fully-qualified names of everything in or below its argument (which is defaulted to do both . and key, i.e. everything)&gt;&gt; wtffd gets specifically all the lambdas in the workspace, then recursively searches them for embedded lambdas and gives those unambiguous names&gt;&gt; wtf returns the name of a function matching its argument (intended use is "wtf .z.s" from an error prompt)&gt;&gt; callers is still a little buggy, but is meant to identify where a function is called&gt;&gt; (recursion in general is done with .z.shttps://code.kx.com/trac/wiki/Reference/dotzdots)&gt;&gt; mapd:{y!x y,:()}&gt;&gt; dfilter:{where[x y]#y,:()}&gt;&gt; wtfcat:{$[$[null x;1;99=type get x;~first key x;0b]|.~x;&gt; ?(raze/).z.s each'x .Q.dd''key each x:$[null x;.;x];x]}&gt;&gt; wtffd:{(!). flip raze&gt; ?{enlist[(x;y)],raze .z.s'[(x,lambda).Q.dd/:til count p;p@:where 100=type each p:get y]}.'> ? {key,'get x}dfilter[100=type each]mapd[get each]wtfcat x}>> wtf:{wtffd?x}>> callers:{where x{x in get[y]3}'wtffd}> –> Aaron Davies> aaron.dav...@gmail.com</aaron.dav…>