Tell me about it. J one-liners can feel quite formidible. Permit me to
elaborate:
]a=: ?.@$~ 5
4 1 3 1 0
i.@# a
0 1 2 3 4
In other words (i.@#) gives us exactly the list of x arguments to (|.) that we
want. Of course, in this case the (@) isn't necessary, but explicitly composing
in this way makes the algebra more direct.
Anyway, with the above we naively want to write something like this:
(i.@# a) |. a
|length error
| (i.@#a) |.a
However, dyadic (|.) actually takes a *list* as it's left argument:
|.b.0
_ 1 _
So a brute force solution would be
(i.@# a) |."0 _ a
4 1 3 1 0
1 3 1 0 4
3 1 0 4 1
1 0 4 1 3
0 4 1 3 1
But, personally, I consider useage of (") a code smell. In my experience,
finding ways to eliminate (") often leads me to more "natural" solutions.
Anyway, in this case, can we eliminate it?
Well, a lists of lists will get split naturally by the rank-handling engine (is
there a good name for this execution step?). Luckily, there is a primitive for
lifting a "row vector" to a "column vector":
,. i.@# a
0
1
2
3
4
Putting the pieces together, we get something like this:
(,.@i.@# a) |. a
Let's factor out that noun. Here is some nice J algebra that is quite useful:
x u (v y) <--> x (u v) y NB. just your dyadic hook definition
(v x) u y <--> x (u~ v)~ y
(v x) u x <--> (u~ v) x NB. a specialization of the above when (x-:y).
Thus, in our case, this becomes
(|.~ ,.@i.@#) a
Of course, for your array of boxes, we want to each-ify this, so we end up with
(|.~ ,.@i.@#)&.> n
Hope that helps!
Skip Cave <[email protected]> wrote:
> Wow! That will take me some studying to understand, but that is exactly
> what I needed. Thanks so much!
>
> Skip
>
>
> Skip Cave
> Cave Consulting LLC
>
>
> On Sat, Oct 31, 2020 at 1:33 AM ethiejiesa via Programming <
> [email protected]> wrote:
>
> > What about something like this?
> >
> > (|.~ ,.@i.@#)&.> n
> > ┌─────┬─────────┬───┬───────┐
> > │1 2 3│4 5 6 7 8│8 6│3 5 7 9│
> > │2 3 1│5 6 7 8 4│6 8│5 7 9 3│
> > │3 1 2│6 7 8 4 5│ │7 9 3 5│
> > │ │7 8 4 5 6│ │9 3 5 7│
> > │ │8 4 5 6 7│ │ │
> > └─────┴─────────┴───┴───────┘
> >
> > Skip Cave <[email protected]> wrote:
> > > Is there a more concise way to express m rotations of each of a set of
> > > items?
> > >
> > > ]n=.1 2 3;4 5 6 7 8;8 6;3 5 7 9
> > >
> > > ┌─────┬─────────┬───┬───────┐
> > >
> > > │1 2 3│4 5 6 7 8│8 6│3 5 7 9│
> > >
> > > └─────┴─────────┴───┴───────┘
> > >
> > > m=. 0, 1, 2, 3, 4 NB. Number of rotations
> > >
> > > ea =. each
> > >
> > >
> > > n,.(1|.ea n),.(2|.ea n),.(3|.ea n),.(4|.ea n)
> > >
> > > ┌─────────┬─────────┬─────────┬─────────┬─────────┐
> > >
> > > │1 2 3 │2 3 1 │3 1 2 │1 2 3 │2 3 1 │
> > >
> > > ├─────────┼─────────┼─────────┼─────────┼─────────┤
> > >
> > > │4 5 6 7 8│5 6 7 8 4│6 7 8 4 5│7 8 4 5 6│8 4 5 6 7│
> > >
> > > ├─────────┼─────────┼─────────┼─────────┼─────────┤
> > >
> > > │8 6 │6 8 │8 6 │6 8 │8 6 │
> > >
> > > ├─────────┼─────────┼─────────┼─────────┼─────────┤
> > >
> > > │3 5 7 9 │5 7 9 3 │7 9 3 5 │9 3 5 7 │3 5 7 9 │
> > >
> > > └─────────┴─────────┴─────────┴─────────┴─────────┘
> > >
> > >
> > > Or better yet with no duplicated rotations::
> > >
> > > 2 7$~.,n,.(1|.ea n),.(2|.ea n),.(3|.ea n),.(4|.ea n)
> > >
> > > ┌─────────┬─────┬─────┬─────────┬─────────┬─────────┬─────────┐
> > >
> > > │1 2 3 │2 3 1│3 1 2│4 5 6 7 8│5 6 7 8 4│6 7 8 4 5│7 8 4 5 6│
> > >
> > > ├─────────┼─────┼─────┼─────────┼─────────┼─────────┼─────────┤
> > >
> > > │8 4 5 6 7│8 6 │6 8 │3 5 7 9 │5 7 9 3 │7 9 3 5 │9 3 5 7 │
> > >
> > > └─────────┴─────┴─────┴─────────┴─────────┴─────────┴─────────┘
> > >
> > > Can this be done using iteration rather than explicitly listing each
> > > rotation?
> > >
> > > Skip Cave
> > > Cave Consulting LLC
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm