Tacit Valued Explicit Definitions

From J Wiki
Jump to navigation Jump to search

This page assumes you are familiar with some J jargon: nouns, verbs, adverbs, conjunctions, copula, sentence, tacit and explicit, and was tested in J903.



In some cases, tacit code may result in run-on sentences. When this happens, it can be convenient to break up the definition across multiple lines, giving names -- perhaps temporary names -- to component parts and then assembling them into a final result.

Here, the mechanics of direct definitions can be useful, as they allow locally defined names which are discarded when the definition completes.

Thus, for example,

 load'gl2'
 glqtextmetrics=: (1&{::[chkgl2)@(('"',libjqt,'" glqtextmetrics ',(IFWIN#'+'),' i *i')&cd@(<7#00))"1

This is a call to a library routine named glqtextmetrics. The library signature is long, and can distract the eye when reading the definition.

Here's an alternative approach, abusing a direct definition:

 load'gl2'
 glqtextmetrics=: _{{
   m=. '"',libjqt,'" glqtextmetrics ',(IFWIN#'+'),' i *i'
   u=. m&cd@(<7#00)
   (1&{:: [ chkgl2)@u"1
 }}

Here, m is the library signature. This is the argument to cd which identifies the external library being called (the value of libjqt), the name of the library entry (glqtextmetrics) and the result and argument types (either + i * or i *i).

In this particular case, we always call this routine with an argument consisting of 7 integer zeros, which are placeholders representing the memory which will hold the constructed result from that routine. In this definition, we check the result for errors and we return that constructed result.

The direct definition here is an adverb definition (because it has a variable named u, but no variable named v). We do not use the adverb argument, so we use _ for that argument (which will be ignored). The result is the same as the long single line definition but the lines are shorter (which avoids email line wrap).

This kind of approach can also be used with gerund definitions. For example, the infamous fizz buzz could be implemented this way:

 fizzbuzz=: _{{
   m=. ":@]`('Fizz'"_)`('Buzz'"_)`('FizzBuzz'"_)
   u=. m@.(2 #. 0 = 5 3&|)
   u"0@>:@i.
 }}

Summary

A J definition which produces a verb can be used when defining J verbs. Obvious, right?