www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Is there a smart way to process a range of range by front ?

reply BBasile <bb.temp gmx.com> writes:
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 ?
Sep 23 2015
next sibling parent BBasile <bb.temp gmx.com> writes:
On Wednesday, 23 September 2015 at 20:44:07 UTC, BBasile wrote:
 Is it possible ?
sorry, I meant to post this in .learn
Sep 23 2015
prev sibling parent reply thedeemon <dlang thedeemon.com> writes:
On Wednesday, 23 September 2015 at 20:44:07 UTC, BBasile wrote:
 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:
 Is it possible ?
What exactly shall your function do? How is it different from and http://dlang.org/phobos/std_range.html#roundRobin ?
Sep 23 2015
parent BBasile <bb.temp gmx.com> writes:
On Thursday, 24 September 2015 at 04:26:05 UTC, thedeemon wrote:
 On Wednesday, 23 September 2015 at 20:44:07 UTC, BBasile wrote:
 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:
 Is it possible ?
What exactly shall your function do? How is it different from and http://dlang.org/phobos/std_range.html#roundRobin ?
http://forum.dlang.org/thread/lwehxuaarulmyiquoqsu forum.dlang.org problem solved (transposed.joiner) [1]. If someone cleans the NG server some time to time this topic should be marked for.
Sep 23 2015