Thursday, May 19, 2016

github.com/adavies42/qist

q-ist is now on GitHub! I’ve started a new repository, https://github.com/adavies42/qist. My old code from contrib is now available there, but more importantly, I received permission from work to release a collection of utilities and example code from my personal library. This includes a much fuller-featured version of my wtf function, my awq tool for using q as a text filter, dozens of miscellaneous utility functions, and more. I don’t have a README for the whole repository written yet, so to get you started, most of the interesting stuff is in lib, except for awq, which is in bin. Have fun exploring the repository, and feel free to comment or email me with any questions about the code.

Labels:

Friday, October 16, 2015

A Combinatoric Combinator

I wrote this while I was playing around with using q on a hobby project, and I thought I’d share it in case anyone else might find it useful.
It takes a number k, a function f, and a list or dictionary y of count n, and runs f once for each of the k-combinations of y. The result is returned as a dictionary with the function outputs as its values and its keys determined by the type of y: if y is a list, its keys are the subsets of y that produced the outputs; if y is a dictionary, its keys are the subsets of the keys of y that index the subsets of y that produced the outputs.

eachc:{
    c:(where reverse 0b vs)each c@:where((first x)=sum 0b vs)each c:til"j"$2 xexp count y;
    (last x)peach$[99h=type y;(key each y)!y:y{((key x)y)#x}/:c;y!y:y@/:c]}

Examples:
q)eachc[(3;sum)]til 5
0 1 2| 3
0 1 3| 4
0 2 3| 5
1 2 3| 6
0 1 4| 5
0 2 4| 6
1 2 4| 7
0 3 4| 7
1 3 4| 8
2 3 4| 9
q)eachc[(3;sum)]`a`b`c`d`e!til 5
a b c| 3
a b d| 4
a c d| 5
b c d| 6
a b e| 5
a c e| 6
b c e| 7
a d e| 7
b d e| 8
c d e| 9
q)
Notes and caveats:
On little-endian machines (i.e. Sparc), the reverse will probably need to be removed.
The size of the result set gets very big very quickly—an n of thirty is probably infeasible for most machines.
I’ve written it to execute f on the combinations with peach, rather than each; this may or may not be appropriate, depending on the nature of any given f and y.

Labels:

Monday, June 4, 2012

What the Function?!?

q is notorious for its limited debugging features. One prominent aspect of this is the way runtime errors in the interactive shell are handled: you enter the debug shell and are presented with the body of the function that failed and the error that occurred. What’s missing? The name of the function!

So, to fill that gap, I’ve written a function wtf[] which will search the workspace for its argument, returning a fully-qualified name if it finds anything. wtf, along with brief documentation and a couple examples, can now be found in the lib subdirectory of my contrib (a section I hope to expand in the future).

Labels: ,

Monday, May 21, 2012

JSON

I've just added a small JSON parser I wrote a while back to my contrib: http://code.kx.com/wsvn/code/contrib/adavies/json/.

Labels:

Thursday, April 12, 2012

contrib/adavies

My public q code can, for the moment, be found in my part of code.kx.com's contrib section.

So far, there's a basic P&L calculator and a C extension for retrieving thread ids, but more should be showing up in the near future.

Labels: