Is there a smart way to process a range of range by front ?

BBasile via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Sep 23 13:44:05 PDT 2015


I was thinking to a general *interleave()* algorithm for any 
compatible Range of Range but I can't find any smart way to 
process each sub range by front, eg:

---
void interleave(RoR)(RoR r)
{
    r.each!(a => a.writeln);
}

void main()
{
     auto r = [[0,2],[1,3]];
     interleave(r);
}
---

will print:
[0,2]
[1,3]

while to interleave i need to take the front of each sub range 
before poping each ror element.

Currently I'm here (don't run this ;)) :

---
auto interleave(RoR)(RoR r)
{
     alias T = ElementType!r[0];
     T[] result;
     while (!empty(r[0]))
         r.each!(a => (result ~= a.front, a.popFront));
     return result;
}

void main()
{
     auto r = [[0,2],[1,3]];
     interleave(r);
}
---

but it doesn't work because 'a' is not consumed. It looks like 
it's saved from the input parameter at each iteration of the 
while loop hence it never returns.

Is it possible ?


More information about the Digitalmars-d-announce mailing list