I came up with this solution which uses amend (}) and iterate (^:_)
before I saw that many more replies had come in:

fill =: ( ((] {~ <:@:(I.@:=))`(I.@:=)`])}  ) ^:_
   0 fill 0 0 1 0 2 0 0 3 0 0 0 4 0 0 0 0
4 4 1 1 2 2 2 3 3 3 3 4 4 4 4 4

This is not ideal because of the way it handles the first elements. To
handle the case where  x = {. y I tried to extend it with something
like this:
fill2 =: fill &. (1&,)

but I could not get it to work. If I could get that to work then I
would make fill an adverb which inputs the value to use for the
beginning in cases where x = {. y.

Something like:

fill =: 1 : 0
:
 ( ( ((] {~ <:@:(I.@:=))`(I.@:=)`])}  ) ^:_  ) &. ([: m&,])
)

It is important to be able to specify what value gets used at the
start. As it stands, ( ((] {~ <:@:(I.@:=))`(I.@:=)`])}  ) ^:_  does
not handle this very well. I did not compare how fast it is, it is
probably very slow since it uses ^:_ ?


On Wed, Oct 29, 2008 at 6:30 PM, Zsbán Ambrus <[EMAIL PROTECTED]> wrote:
> On Wed, Oct 29, 2008 at 16:21, Raul Miller <[EMAIL PROTECTED]> wrote:
>>   f=: ] {~ ~: >./\@:* [EMAIL PROTECTED]@]
>
> Nice.
>
> I wondered if I could use prefix (u\ monad) somehow, like this:
>
>   0 (i:&1\@:~: { ]) 1 0 2 3 4 5 0 0 0 4 3 2 4
> 1 1 2 3 4 5 5 5 5 4 3 2 4
>
> but then I decided I couldn't, for it would make the solution quadratic-time.
>
> Yours however uses >./\ which has special code to run in linear time,
> thus avoiding the problem.
>
> Ambrus
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to