Here's how I would handle the empty result case:

mergeMask=: [: ;@(+/\@(0 1&E.) <@(* ~:/\)/. ]) -@# {. 2 =/\ ]

(mergeRow remains unchanged)

Thanks,

-- 
Raul

On Tue, Jun 7, 2016 at 6:10 PM, Ric Sherlock <[email protected]> wrote:
> Thanks that's much nicer.
> The only case it doesn't handle is the empty arg (which I didn't put in my
> spec!).
>
>    mergeRow i.0
>
> |length error: mergeRow
>
> | mergeRow i.0
>
>
> A quick fix:
> mergeRow=: ((-.@] # [ (+/@:{~ <:,:])`(<:@])`[} I.@]) mergeMask) :: ((i.0)"_)
>
>    $mergeRow i.0
>
> 0
>
>
>
>
>
> On Wed, Jun 8, 2016 at 9:29 AM, Raul Miller <[email protected]> wrote:
>
>> Best I can think of is something like this:
>>
>> mergeMask=: [: ;@(+/\@(0 1&E.) <@(* ~:/\)/. ]) 0, 2 =/\ ]
>> mergeRow=: (-.@] # [ (+/@:{~ <:,:])`(<:@])`[} I.@]) mergeMask
>>
>> Hopefully that makes sense?
>>
>> Thanks,
>>
>> --
>> Raul
>>
>>
>> On Tue, Jun 7, 2016 at 5:15 PM, Ric Sherlock <[email protected]> wrote:
>> > I'm looking for a J-like approach to a verb for merging adjacent numbers
>> in
>> > a row where the numbers are the same. For example
>> >
>> > mergerow 4 4 4 4
>> >
>> > 8 8
>> >
>> > mergerow 8 8
>> >
>> > 16
>> >
>> > mergerow 16
>> >
>> > 16
>> >
>> > mergerow 4 4 4
>> >
>> > 8 4
>> >
>> > mergerow 8 4
>> >
>> > 8 4
>> >
>> > mergerow 4 8 8 4
>> >
>> > 4 16 4
>> >
>> >
>> > My version below which I've used as part of the answer to a RosettaCode
>> task
>> > <http://rosettacode.org/wiki/2048#J> is very non J-like:
>> >
>> > mergerow=: verb define
>> >   len=. <:@# y
>> >   row=. i. i=. 0
>> >   while. (i <: len) *. len >: 0 do.
>> >     if. =/ (i , i+1) { y,_9 do.
>> >       row=. row, +: i { y
>> >       i=. i + 2
>> >     else.
>> >       row=. row, i { y
>> >       i=. i + 1
>> >     end.
>> >   end.
>> >   row
>> > )
>> >
>> > I thought I had a good solution with:
>> >
>> > mergerow=: rplc (2&# ; +:)&>@~.
>> >
>> > But this seemed to cause unpredictable behaviour when used as part of the
>> > whole solution.
>> > ----------------------------------------------------------------------
>> > 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

Reply via email to