jkdb – kdb Java – Running q language code within the java runtime

While doing the project euler programming challenges  it annoyed me how verbose the java answers would have to be compared to kdb. Then I got to wondering if I could create functions like til,mod,where,asc etc. in java and use them to create really short answers. Once I had the basic functions working, I wondered if I could get a working q) prompt…

I ended up with a prompt that let me run the following valid q code within the java runtime:
where[or[ =[0; mod[til[1000];3]]; =[0; mod[til[1000];5]] ]] 

Download the jkdb functional kdb code

The Code

How it Works

My Jkdb class has a number of statically defined functions that accept arrays of ints/doubles and return arrays/atoms of int/double. An example is asc:

In the background each line you type has the square brackets and semi-colons converted to () and comma which are now valid java function calls. The amended string gets added to a .java file, compiled, ran and the output shown each time you press enter. Since I statically imported my class that has the til(x),max(x) etc functions, the code is valid java and works. This is why we can only use functional[x;y] form calls and not the infix notation x func y as I didn’t want to write a parser.

Interesting Points that would occur in Kdb

  • All of the functions usually accept int,double, int[],double[] and boolean[] as their arguments and the various combinations. It is annoying having to copy paste these various combinations, I assume in kdb they use C macros to generate the functions, we could do something similar by writing java code to write the java functions.
  • The functions I wrote often modify the array passed in, this gives us insight to why in the kdb C api you have to reference count properly, when the reference is 0 the structure is probably reused.

What use is this?

Almost entirely useless 🙂 I use the java class to occasionally spit out some sample data for tests I write. It was however insightful to get an idea of how kdb works under the covers. If you found this interesting you might also like kona an open source implementation of kdb.

I’d be interested in hearing:

  • Have you tried kona? Did it work well?
  • Have you found a functional library that gives you similar expressiveness to kdb for java?
  • Tried Scala? Clojure?

The Jkdb provided functions are:

til where
equal=
index@
choose(x,y) ? – only supports x and y as int’s to choose random numbers
or(x,y) and(x,y)
mul(x,y) add(x,y)
mod(x,y)
floor ceiling abs
cos sin tan
acos asin atan
sqrt
asc desc max min reverse
sum sums prd prds
set(String key, Object value)
get(String key)

1 Response to “jkdb – kdb Java – Running q language code within the java runtime”


  1. Hakan Kjellerstrand

    Nice experiment with JKDB.

    I’m the one you linked to for the Kona link above. The official Kona site is https://github.com/kevinlawler/kona . Here’s some more Euler solutions: https://github.com/kevinlawler/kona/wiki/Project-Euler-Code-Golf . The idiom page: https://github.com/kevinlawler/kona/wiki/Idioms .

    Some Kona (K) versions of Euler#1:

    +/&~&/(!1e3)!/:3 5
    +/’!1e3
    +/’!1e3

    (By the way, your JKDB example of Euler#1 have two 3s. The latter should be a 5.)

    Kona is – what I have understood – not as fast as Kx:s Q/Kdb but it’s quite nice (and open source). Right now I prefer Kona to Q/Kdb since it’s a little terser and suites my mind quite well. Q has many nice features not in Kona and it is on my TODO list to explore more (I bought the Mortals book some months ago).

    /Hakan