Mikl Kurkov wrote:
> I have task for which I couldn't find a solution.

Some background about the larger problem into which this fits 
might be useful?

> Suppose we have some indexes which relate to some data.
> Now I have vector which describe which indexes to keep 
> and which to remove.
>
> For example:  pat =: 4 _3 2 _1

Without knowing more about the problem, "indices" seems like
it might just as well be "values".

> Positive value - tell how many indexes to keep, negative to remove. 
> So the pat is telling that I need to keep 4 first indexes then 
> remove 3 next, keep another 2, remove 1, keep 4, remove 3 and so on.
> I need function that for giving index give me new index after 
> applying such pattern. Indexes of removed items - is index of next 
> keeped item.
>
> So applying this function to i.20 and pat I should see:
> OLD: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> NEW: 0 1 2 3 4 4 4 4 5 6 6  7  8  9  10 10 10 10 11 12

So the pattern repeats?

First off, consider:
   shift=: _1 |.!.0 ]
   +/\ shift (|#0<])4 _3 2 _1 
0 1 2 3 4 4 4 4 5 6

You can get the above from:
   +/\ shift (|#0<])4 _3 2 _1 4 _3 2 _1
0 1 2 3 4 4 4 4 5 6 6 7 8 9 10 10 10 10 11 12

To get what I think you are asking for, you need to extend the
pattern to the proper length.

To get the more general result, I guess you would use the above as
a left argument to { but I may have misunderstood what you are
asking for.

It seems to me that given a list of length n and a pattern p, that
you want >.n%+/|p copies of the pattern.  This will sometimes give
you a set of indices which are too long, so use n{. after generating
the sequence of indices.

-- 
Raul

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

Reply via email to