Justin Paston-Cooper wrote:
> Is there an abbreviation for the following fork? I don't want the
> computation of <<x b y>> to happen twice.
>
> ([ a b) c (] d b)

Raul responded:
> When you need to intermix three (or more) values in J,
>  you have several options:

> [1] Compose some (or all) of them into a list
> [2] Use an explicit definition [i.e. named nouns]
> [3] recompute some of them

(I'd never seen the options for >2 verb arguments spelled out like this
before -- I like it.) 

Raul is right, J verbs are limited to 1 or 2 inputs.  

If you wanted to reuse (the output of) b in this fork:

        ([ a b) c (] d b)

along the lines of  

        (something b)

where the "something" contains a reference to x, y, and the output of b
(twice), then the trouble becomes obvious. The "something" has only 2
inputs, but needs access to 3.

Let's look at the 3 remedies Raul outlined:

> [3] recompute some of them

         ([ a b) c (] d b)  

Computes b twice (i.e. that's what you're already doing).

> [2] Use an explicit definition [i.e. named nouns]

   dyad def '(x a B) c y d~ B=.x b y'

Names a noun & reuses it.

(Of course, you don't need an explicit definition for this; you could use
global named nouns.  But local nouns are cleaner for ephemera.)

> [1] Compose some (or all) of them into a list

        ,&< ((>@:{.@:[ a ]) c >@:{:@:[ d ]) b

Composes x and y into a list & later decomposes the list.  The output of b
is reused.

Of course, this latter verb is much uglier & harder to maintain than the
original.  If you prefer the notation of the original, but the (presumed)
efficiency gains of reusing the output of b, then see [1].  

In that message, I presented a conjunction, "reuse", that takes a verb
(train) on the left (u) and a verb on the right (v), and outputs a verb
equivalent to u except multiple invocations of v are replaced with a single
invocation, and the output reused.  

Applied to your train, reusing "b" :

           ( ([ a b) c (] d b) ) reuse b
        ,&< ((>@:{.@:[ a ]) c >@:{:@:[ d ]) b

Note the verb output is the same as I presented just above.  Now you can
maintain the "clear" version of the verb, but still get the (presumed)
benefits of the "obscure" version.  Of course, the "clear" version still
(notationally) uses b twice, which is not entirely satisfying.

But, there it is.

-Dan

[1]  "How to use dyad I." .  See the conj  reuse  :
      http://www.jsoftware.com/pipermail/programming/2010-April/019189.html


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to