[Jprogramming] reshaping a list to a table

'Michael Day' via Programming programming at jsoftware.com
Sat May 16 15:14:25 UTC 2020


Much neater than what I was about to offer,  unless Raoul needs to 
specify the fill,

in which case,  this alternative rather minimal amendment is worth 
consideration:

    ($!._ ~2,~>. at -:@#) i.7
0 1
2 3
4 5
6 _

cf

    _2]\ i.7
0 1
2 3
4 5
6 0

Also, the time and space performance _might_ be important for large inputs:

    ts' $ _2]\ list ' [list =: i.100000
0.000618 1.04986e6
    ts'($!._ ~2,~>. at -:@#) list'
2.7e_6 2304

Cheers,

Mike

On 16/05/2020 15:57, 'Rob Hodgkinson' via Programming wrote:
> You could try Infix … here with NuVoc link…
> https://code.jsoftware.com/wiki/Vocabulary/bslash#dyadic
>
>     x u\ y where x is eg _2 means apply very b to successive pairs (_ for non-overlapping).
>
>       _2 ]\ 1 2 3 4 5 6
> 1 2
> 3 4
> 5 6
>       _2 ]\ 1 2 3 4 5 6 7
> 1 2
> 3 4
> 5 6
> 7 0
>
>      _2 <\ 1 2 3 4 5 6
> ┌───┬───┬───┐
> │1 2│3 4│5 6│
> └───┴───┴───┘
>
> HTH…/Rob
>
>> On 17 May 2020, at 12:42 am, Raoul Schorer <raoul.schorer at gmail.com> wrote:
>>
>> Hello,
>>
>> I am convinced that this must be trivial, but I wasn't able to find in the documentation how to reshape a list to a table without manually extracting the length.
>>
>> in summary, is there a more direct way of doing:
>>
>> lst =. i. 6
>>
>> ((2,~2%~#) $ ]) lst
>>
>> for a list of arbitrary length?
>>
>> Thanks!
>>
>> Raoul
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm


-- 
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


More information about the Programming mailing list