From [Tuple!(A,B), ...] to Tuple!(A[], B[])
    FeepingCreature 
    feepingcreature at gmail.com
       
    Mon Feb 17 11:51:52 UTC 2020
    
    
  
On Monday, 17 February 2020 at 11:07:33 UTC, foozzer wrote:
> Hi all,
>
> There's something in Phobos for that?
>
> Thank you
Here you go:
import std;
// extract the types that make up the tuple
auto transposeTuple(T : Tuple!Types[], Types...)(T tuples)
{
     // templated function that extracts the ith field of an array 
of tuples as an array
     auto extractArray(int i)()
     {
         return tuples.map!(a => a[i]).array;
     }
     // return tuple of calls to extractArray, one for each tuple 
index
     return tuple(staticMap!(extractArray, 
aliasSeqOf!(Types.length.iota)));
}
void main() {
     Tuple!(int, double)[] array;
     array ~= tuple(1, 2.0);
     array ~= tuple(3, 4.0);
     Tuple!(int[], double[]) tuple = array.transposeTuple;
     assert(tuple[0] == [1, 3]);
     assert(tuple[1] == [2.0, 4.0]);
}
    
    
More information about the Digitalmars-d-learn
mailing list