{"id":107,"date":"2014-12-22T15:46:42","date_gmt":"2014-12-22T15:46:42","guid":{"rendered":"http:\/\/procyonic.org\/blog\/?p=107"},"modified":"2014-12-22T15:46:42","modified_gmt":"2014-12-22T15:46:42","slug":"optional-keyword-arguments-in-j","status":"publish","type":"post","link":"https:\/\/procyonic.org\/blog\/optional-keyword-arguments-in-j\/","title":{"rendered":"Optional Keyword Arguments in J"},"content":{"rendered":"<p>J is great!  It is a wonderful little language for data analysis tasks.  However, to programmers used to working in modern dynamic languages, it sometimes feels a little restrictive.  In particular, a ubiquitous feature in the post-Python (let&#8217;s call it) era is hash-maps.  Even in older languages, like Common Lisp, the association list &#8211; allowing abritrary mappings between keys and values, is a very common idiom.<\/p>\n<p>J exposes no native functionality that exactly meets this use case, one common application of which is named, optional arguments to functions.  <\/p>\n<p>In developing an <a href=\"\">interactive gnuplot interface<\/a> for J, I wanted to pass optional keyword arguments to functions so that plots can be easily customized.  So I developed a simple representation of key\/val pairs which is efficient enough for small collections of named values.<\/p>\n<p>Consider:<\/p>\n<pre><code>nil =: (0$0)\r\n\r\nrankOne =: 1: -: (#@:$)\r\nrankTwo =: 2: -: (#@:$)\r\ntalliesEven =: 0: -: (2: | #)\r\ntwoColumns =: 2: -: 1&amp;{@:$\r\n\r\nopts =: monad define\r\n  keysandvals =. y\r\n  assert. rankOne keysandvals\r\n  assert. talliesEven keysandvals\r\n  ii =. 2 | (i. #y)\r\n  keys =. (-. ii) # y\r\n  vals =. ii # y\r\n  keys (&gt;@:[ ; ])\"(0 0) vals\r\n)\r\n<\/code><\/pre>\n<p>Opts is a monadic verb which takes a flat boxed array of even length and returns a rank two boxed array whose first column is keys and whose second column is values:<\/p>\n<pre><code>opts 'key1';10;'key2';11\r\n+----+--+\r\n|key1|10|\r\n+----+--+\r\n|key2|11|\r\n+----+--+\r\n<\/code><\/pre>\n<p>Look up is simple: find the key&#8217;s index in the first column and index the second column with it.  Return nil if the key isn&#8217;t found.<\/p>\n<pre><code>getopt =: dyad define\r\n  options =. x\r\n  key =. y\r\n  assert. rankTwo options\r\n  assert. twoColumns options\r\n  if. 0 -: #options do.\r\n   nil\r\n  else. \r\n    ii =. ((i.&amp;1)@:(((key&amp;-:@:&gt;)\"0)@:(((0&amp;{)@:|:)))) options\r\n    if. ii &lt; #options do.\r\n      (&gt;@:(ii&amp;{)@:(1&amp;{)@:|:) options\r\n    else.\r\n      nil\r\n    end.\r\n  end.\r\n)\r\n<\/code><\/pre>\n<p>Eg:<\/p>\n<pre><code>(opts 'key1';10;'key2';11) getopt 'key1'\r\n-&gt; 10\r\n<\/code><\/pre>\n<p>We can now define a handy conjunction to allow the specification of a default value:<\/p>\n<pre><code>dft =: conjunction define \r\n:\r\n  r =. x u y\r\n  if. r -: nil do.\r\n   n\r\n  else.\r\n   r\r\n  end.\r\n)\r\n<\/code><\/pre>\n<p>Which we use this way:<\/p>\n<pre><code>(opts 'key1';10;'key2';11) getopt dft '___' 'key1'\r\n-&gt; 10\r\n(opts 'key1';10;'key2';11) getopt dft '___' 'key3'\r\n'---'\r\n<\/code><\/pre>\n<p>This allows us to pass optional arguments to verbs and specify default values relatively easily, as in this example from my gnuplot library:<\/p>\n<pre><code>histogram =: verb define \r\n  (ensureGnuPlot'') histogram y\r\n:\r\n  data =. y\r\n  if. boxedP x do.\r\n   options =. x\r\n   gph =. options getopt dft (ensureGnuPlot'') 'gnuplot'\r\n  else.\r\n   gph =. x\r\n   options =. (opt '')\r\n  end.\r\n  mn =. &lt;.\/ data\r\n  mx =. &gt;.\/ data \r\n  bw =. options getopt dft (0.1&amp;* mx-mn) 'binwidth'\r\n  pttl =. options getopt dft '' 'plot-title'\r\n  'binwidth=%f\\n' gph gpfmt &lt;bw\r\n  gph send 'set boxwidth binwidth'\r\n  gph send 'bin(x,width)=width*floor(x\/width) + binwidth\/2.0' \r\n  s =. 'plot \"%s\" using (bin($1,binwidth)):(1.0) smooth freq title \"%s\" with boxes'\r\n  s gph gpfmt ((asFile (asVector data));pttl)\r\n)\r\n<\/code><\/pre>\n<p>Where, in the dyadic case, we detect whether <code>x<\/code> is boxed, and if so, treat it as a list of options.  We extract each option by name, providing a reasonable default.<\/p>\n<p>This seems like a common problem, so I am wondering if anyone else in the J community has solved it before in a different way?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>J is great! It is a wonderful little language for data analysis tasks. However, to programmers used to working in modern dynamic languages, it sometimes feels a little restrictive. In particular, a ubiquitous feature in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[18,47],"tags":[22,20,21,3],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-j","category-programming","tag-alists","tag-j-2","tag-optional-arguments","tag-programming"],"_links":{"self":[{"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":1,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/procyonic.org\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}