{"id":36,"date":"2014-03-30T21:46:42","date_gmt":"2014-03-30T21:46:42","guid":{"rendered":"http:\/\/www.timestored.com\/b\/?p=36"},"modified":"2014-03-30T21:46:42","modified_gmt":"2014-03-30T21:46:42","slug":"jkdb-java-kdb-functional-library","status":"publish","type":"post","link":"https:\/\/www.timestored.com\/b\/jkdb-java-kdb-functional-library\/","title":{"rendered":"jkdb &#8211; kdb Java &#8211; Running q language code within the java runtime"},"content":{"rendered":"<p>While doing the <a href=\"http:\/\/projecteuler.net\" target=\"_blank\">project euler<\/a> programming challenges\u00a0 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&#8230;<\/p>\n<p>I ended up with a prompt that let me run the following valid q code within the java runtime:<br \/>\n<code>where[or[ =[0; mod[til[1000];3]]; =[0; mod[til[1000];5]] ]]\u00a0<\/code><\/p>\n<div class=\"media\"><object id=\"csSWFb\" width=\"640\" height=\"350\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"scale\" value=\"showall\" \/><param name=\"allowfullscreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"flashvars\" value=\"tocdoc=left&amp;showsearch=true&amp;autostart=false&amp;autohide=true&amp;content=\/b\/videos\/jkdb-java-kdb.mp4&amp;thumb=\/b\/videos\/jkdb-java-kdb.png&amp;containerwidth=640&amp;containerheight=350&amp;smoothing=true&amp;enablejsapi=true&amp;fullscreen=true&amp;windowbox=false&amp;showbranding=false&amp;showstartscreen=true&amp;showendscreen=false&amp;basecolor=272727&amp;loop=false\" \/><param name=\"src\" value=\"\/js\/video_controller.swf\" \/><embed id=\"csSWFb\" width=\"640\" height=\"350\" type=\"application\/x-shockwave-flash\" src=\"\/js\/video_controller.swf\" scale=\"showall\" allowfullscreen=\"true\" allowscriptaccess=\"always\" flashvars=\"tocdoc=left&amp;showsearch=true&amp;autostart=false&amp;autohide=true&amp;content=\/b\/videos\/jkdb-java-kdb.mp4&amp;thumb=\/b\/videos\/jkdb-java-kdb.png&amp;containerwidth=640&amp;containerheight=350&amp;smoothing=true&amp;enablejsapi=true&amp;fullscreen=true&amp;windowbox=false&amp;showbranding=false&amp;showstartscreen=true&amp;showendscreen=false&amp;basecolor=272727&amp;loop=false\" \/><!--[if !IE]>--> <img loading=\"lazy\" src=\"https:\/\/www.timestored.com\/b\/wp-includes\/js\/tinymce\/themes\/advanced\/img\/trans.gif\" class=\"mceItemMedia mceItemFlash\" width=\"639\" height=\"360\" data-mce-json=\"{'video':{},'params':{'scale':'showall','allowfullscreen':'true','allowscriptaccess':'always','flashvars':'tocdoc=left&amp;showsearch=true&amp;autostart=false&amp;autohide=true&amp;content=\/kdb-guides\/videos\/dans-python-api.mp4&amp;thumb=\/b\/videos\/jkdb-java-kdb.png&amp;containerwidth=640&amp;containerheight=350&amp;smoothing=true&amp;enablejsapi=true&amp;fullscreen=true&amp;windowbox=false&amp;showbranding=false&amp;showstartscreen=true&amp;showendscreen=false&amp;basecolor=272727&amp;loop=false','src':'\/js\/video_controller.swf'},'name':null,'object_html':'&lt;!--&lt;![endif]--&gt;&lt;div&gt;&lt;p&gt;The video content presented here requires JavaScript to be enabled and the latest version of the Adobe Flash Player. If you are using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Adobe Flash Player by &lt;a href=\\&quot;http:\/\/www.adobe.com\/go\/getflashplayer\\&quot;&gt;downloading here&lt;\/a&gt;.&lt;\/p&gt;&lt;\/div&gt;&lt;p&gt;&lt;!--[if !IE]&gt;--&gt;&lt;\/p&gt;','hspace':null,'vspace':null,'align':null,'bgcolor':null}\" alt=\"\" \/><!--<![endif]--><\/object><\/div>\n<p><a href=\"https:\/\/www.timestored.com\/b\/wp-content\/uploads\/2013\/03\/jkdb.zip\">Download the jkdb functional kdb code<\/a><\/p>\n<h4>The Code<\/h4>\n\r\n<textarea class=\"code\" id=\"q-code-1\" cols=\"20\" rows=\"2\">\r\n\/ javac com\/timestored\/Jkdb.java\r\n\/ java com.timestored.Jkdb\r\n\r\n\/\/ find the top 5 numbers in a 100 item random list\r\n@[reverse[asc[?[100;100]]]; til[5]]\r\n\r\n\/\/ set \/ get\r\nset[\"a\"; max[ *[10; til[1000]] ]]\r\n+[getd[\"a\"]; 11]\r\n\r\n\/\/ If we list all the natural numbers below 10 that are multiples of 3 or 5\r\n\/\/ we get 3, 5, 6 and 9. The sum of these multiples is 23.\r\n\/\/ Find the sum of all the multiples of 3 or 5 below 1000.\r\n=[0; mod[til[1000]; 3]]\r\nsum[where[or[ =[0; mod[til[1000]; 3]]; =[0; mod[til[1000]; 3]]]]]\r\n<\/textarea>\r\n<script type=\"text\/javascript\">\/\/ <![CDATA[\r\nCodeMirror.fromTextArea(document.getElementById(\"q-code-1\"), {\r\n        lineNumbers: true,\r\n        matchBrackets: true,\r\n        mode: \"text\/x-plsql\", readOnly:true });\r\n\/\/ ]]><\/script>\r\n\n<h4>How it Works<\/h4>\n<p>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:<br \/>\n\r\n<textarea class=\"code\" id=\"java-code\" cols=\"20\" rows=\"2\">\r\n\/** perform ascending sort **\/\r\npublic static double[] asc(double[] x) { Arrays.sort(x); return x; };\r\npublic static int[] asc(int[] x) { Arrays.sort(x); return x;};\r\npublic static double asc(double x) { return x; }\r\npublic static double asc(int x) { return x; }\r\n<\/textarea>\r\n<script type=\"text\/javascript\">\/\/ <![CDATA[\r\nCodeMirror.fromTextArea(document.getElementById(\"java-code\"), {\r\n        lineNumbers: true,\r\n        matchBrackets: true,\r\n        mode: \"text\/x-java\", readOnly:true });\r\n\/\/ ]]><\/script>\r\n<br \/>\nIn 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&#8217;t want to write a parser. <\/p>\n<h4>Interesting Points that would occur in Kdb<\/h4>\n<ul>\n<li>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.<\/li>\n<li>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.<\/li>\n<\/ul>\n<h4>What use is this?<\/h4>\n<p>Almost entirely useless \ud83d\ude42 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.\u00a0If you found this interesting you might also like <a href=\"http:\/\/www.hakank.org\/k\/\">kona an open source implementation of kdb<\/a>.<\/p>\n<p>I&#8217;d be interested in hearing:<\/p>\n<ul>\n<li>Have you tried kona? Did it work well?<\/li>\n<li>Have you found a functional library that gives you similar expressiveness to kdb for java?<\/li>\n<li>Tried Scala? Clojure?<\/li>\n<\/ul>\n<h4>The Jkdb provided functions are:<\/h4>\n<table>\n<tbody>\n<tr>\n<td>til where<\/td>\n<\/tr>\n<tr>\n<td>equal=<\/td>\n<\/tr>\n<tr>\n<td>index@<\/td>\n<\/tr>\n<tr>\n<td>choose(x,y) ? &#8211; only supports x and y as int&#8217;s to choose random numbers<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<tr>\n<td>or(x,y) and(x,y)<\/td>\n<\/tr>\n<tr>\n<td>mul(x,y) add(x,y)<\/td>\n<\/tr>\n<tr>\n<td>mod(x,y)<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<tr>\n<td>floor ceiling abs<\/td>\n<\/tr>\n<tr>\n<td>cos sin tan<\/td>\n<\/tr>\n<tr>\n<td>acos asin atan<\/td>\n<\/tr>\n<tr>\n<td>sqrt<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<tr>\n<td>asc desc max min reverse<\/td>\n<\/tr>\n<tr>\n<td>sum sums prd prds<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<\/tr>\n<tr>\n<td>set(String key, Object value)<\/td>\n<\/tr>\n<tr>\n<td>get(String key)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>While doing the project euler programming challenges\u00a0 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[2],"tags":[21,96,19,90,20],"_links":{"self":[{"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":32,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":51716,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/posts\/36\/revisions\/51716"}],"wp:attachment":[{"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.timestored.com\/b\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}