1

Consider the following, which computes the successive cross products 0 cross 0 (1 result), then 0 1 cross 0 1 (4 results), then 0 1 2 cross 0 1 2 (9 results):

f=. (<@,"0/~&i.)"0
f 1+i.3

which outputs:

┌───┬───┬───┐
│0 0│   │   │
├───┼───┼───┤
│   │   │   │
├───┼───┼───┤
│   │   │   │
└───┴───┴───┘

┌───┬───┬───┐
│0 0│0 1│   │
├───┼───┼───┤
│1 0│1 1│   │
├───┼───┼───┤
│   │   │   │
└───┴───┴───┘

┌───┬───┬───┐
│0 0│0 1│0 2│
├───┼───┼───┤
│1 0│1 1│1 2│
├───┼───┼───┤
│2 0│2 1│2 2│
└───┴───┴───┘

Sometimes what we really want is the combined result, that is:

f=. [: (#~~:&a:) [: , (<@,"0/~&i.)"0
f 1+i.3

which outputs:

┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0 0│0 0│0 1│1 0│1 1│0 0│0 1│0 2│1 0│1 1│1 2│2 0│2 1│2 2│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

The fundamental problem is that because each number outputs a different number of results (1, 4, 9, etc), we must arrive at our answer via the circuitous route of getting results with fill, only to immediately flatten and filter away that same fill.

Can we solve this with a reduction?

Another approach would be to think of this process as a reduction, which seems promising (no fill / filter) when we try it with 2 arguments:

(,/&([: ,/ ,"0/~&i.))/ 1 2

which outputs:

0 0  NB. <- result of left arg
0 0  NB. \
0 1  NB.  \  these 4 result of right arg
1 0  NB.  /
1 1  NB. /

However, if we try to reduce with a list of 3 or more elements, we'll get an error because after the first rightmost evaluation, the right argument will no longer be an atom, but the table result (ie, the result above) of the verb applied to the first 2 elements.

We can attempt to avoid this by applying our transformation only to one argument, say the left argument, but then the very last argument won't be processed:

(([: ,/ ,"0/~&i.)@[ , ])/ 1 2 3

which outputs:

0 0
0 0
0 1
1 0
1 1
3 3  NB. almost, but woops: the rightmost argument was not expanded

The question is: Is there a solution to the problem of combining results of different shapes in a single calculation, without filling and filtering?

1 Answer 1

4

I'm hoping there's a clever solution, but the straightforward solution that I would reach for is boxing the differently-shaped results.

   <@f 1+i.3
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││0 0│││0 0│0 1│││0 0│0 1│0 2││
│└───┘│├───┼───┤│├───┼───┼───┤│
│     ││1 0│1 1│││1 0│1 1│1 2││
│     │└───┴───┘│├───┼───┼───┤│
│     │         ││2 0│2 1│2 2││
│     │         │└───┴───┴───┘│
└─────┴─────────┴─────────────┘

Then straighten out the contents of the boxes and unbox.

   ; , &. > <@f 1+i.3
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0 0│0 0│0 1│1 0│1 1│0 0│0 1│0 2│1 0│1 1│1 2│2 0│2 1│2 2│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

And we can reduce the statement slightly by reordering.

   ; <@,@f 1+i.3
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0 0│0 0│0 1│1 0│1 1│0 0│0 1│0 2│1 0│1 1│1 2│2 0│2 1│2 2│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
3
  • That's not bad, Dane. Thanks. Upvoted but I'll leave the question open for a while in case there is something even more clever.
    – Jonah
    Commented Dec 2, 2017 at 11:36
  • I think I may add the following adverb to my J utils: flatly=. 1 :'[: ; <@,@x'. The more I think about your approach, the more it seems like the "right" J answer.
    – Jonah
    Commented Dec 3, 2017 at 2:41
  • I agree that is the general approach for "joining" lists of different lengths.
    – Tikkanz
    Commented Dec 3, 2017 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.