2

Suppose two arrays of different sizes:

N0 =: i. 50
N1 =: i. 500

There should be a way to get the unique pairs, just combine the two. The "simplest" I found was:

]$R =: |:,"2 |: (,.N0) ,"1 0/ N1
25000 2

Which is frankly a butt ugly, baseball bat solution. Is there a more elegant way to do this?

2 Answers 2

2

The pattern of data you're reaching for is a variation on Catalogue. It's the most famous variation, in fact: Cartesian product.

On the Vocabulary listing for Catalogue there's also code for Cartesian product. To get the list you want, just ravel and open the result.

pair=: >@ ,@ { @(,&<)

   $ N0 pair N1
25000 2
2
  • I wrote mine as 13 : '>,{x;y'. I'm never sure of how to use @.
    – MPelletier
    Nov 16, 2012 at 5:01
  • When there's a long series of simple composition, as in this case, Explicit form is nice. I strongly recommend using 4 rather than 13 as the left argument of Explicit, in part to emphasize that this verb is dyadic, but also because 13 is not entirely reliable. It is suitable for exploration, not for production.
    – kaleidic
    Nov 17, 2012 at 4:55
1

I'm in search of the same thing.

I've only came up with the following that are shorter but not prettier:

,/(N0 ,. ])"0 N1

;(N0 ,. ]) &.> N1

or in the form:

;N0&,.&.>N1
2
  • That's not bad. I spoke with kaleidic on Twitter (our 3rd J regular on SO) and we'll have an interesting answer later. I don't want to spoil it here :)
    – MPelletier
    Nov 15, 2012 at 21:03
  • 1
    (Peeked at twitter) Duh! Catalogue! It's obvious when you know it... :)
    – Eelvex
    Nov 15, 2012 at 21:38

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.