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