cperm =: [: , [: < "1 [: > [: ,"0 1/ each/ (_2&}. , [: ,"0 0/ each/ _2&{.)
this creates constrained permutations where each position in the list can only
be drawn from its own set:
cperm 3 4 ; 1 2
┌───┬───┬───┬───┐
│3 1│3 2│4 1│4 2│
└───┴───┴───┴───┘
cperm 0 ; 3 4 ; 1 2; 5
┌───────┬───────┬───────┬───────┐
│0 3 1 5│0 3 2 5│0 4 1 5│0 4 2 5│
└───────┴───────┴───────┴───────┘
cperm 8 9 ;0 ; 3 4 ; 1 2; 5
┌─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
│8 0 3 1 5│8 0 3 2 5│8 0 4 1 5│8 0 4 2 5│9 0 3 1 5│9 0 3 2 5│9 0 4 1 5│9 0 4 2
5│
└─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
The rightmost part (_2&}. , [: ,"0 0/ each/ _2&{.) of the verb is just there to
deal with edge cases of joining the last 2 boxes.
The core is: (,"0 1/ each/)
I thought it was pretty cool to use / twice.
This was useful in solving this problem in sufficiently elegant fashion:
http://www.reddit.com/r/dailyprogrammer/comments/24ypno/572014_challenge_161_medium_appointing_workers/
often the constrained permutation list has overlapping allowable values in each
slot, and in the above resource allocation problem, those happen to indicate
the invalid permutations, and are fairly easily eliminated with ~.
It could also be used ( a bit of a stretch) for games where the first few moves
have been played and are known, but you wish to know the permutations for a
select number of future moves.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm