www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A function to split a range into several ranges of different chunks

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
-----
import std.range;
import std.stdio;

void main ()
{
     auto range = sequence!((a, n) => n);

     // works, but the chunks are all the same length
     auto rngs = range.chunks(4);
     writeln(rngs[0]);
     writeln(rngs[1]);
     writeln(rngs[2]);

     // want this
     auto ranges = range.???(3, 4, 5);
     writeln(ranges[0] == [1, 2, 3]);
     writeln(ranges[1] == [4, 5, 6, 7]);
     writeln(ranges[2] == [8, 9, 10, 11, 12]);
}
-----

Do you know of a simple way to do this? It's essentially similar 
to chunks, except the chunks themselves would not be of the same 
length.

You can think of a good name for '???' here. Maybe this is 
already possible by combining functionality in std.range - but I 
came up short.
Sep 14 2020
parent reply Seb <seb wilzba.ch> writes:
On Monday, 14 September 2020 at 07:49:31 UTC, Andrej Mitrovic 
wrote:
 -----
 import std.range;
 import std.stdio;

 void main ()
 {
     auto range = sequence!((a, n) => n);

     // works, but the chunks are all the same length
     auto rngs = range.chunks(4);
     writeln(rngs[0]);
     writeln(rngs[1]);
     writeln(rngs[2]);

     // want this
     auto ranges = range.???(3, 4, 5);
     writeln(ranges[0] == [1, 2, 3]);
     writeln(ranges[1] == [4, 5, 6, 7]);
     writeln(ranges[2] == [8, 9, 10, 11, 12]);
 }
 -----

 Do you know of a simple way to do this? It's essentially 
 similar to chunks, except the chunks themselves would not be of 
 the same length.

 You can think of a good name for '???' here. Maybe this is 
 already possible by combining functionality in std.range - but 
 I came up short.
You likely want to get involved / raise your support here: https://github.com/dlang/phobos/pull/7600
Sep 14 2020
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On Monday, 14 September 2020 at 09:08:01 UTC, Seb wrote:
 You likely want to get involved / raise your support here:

 https://github.com/dlang/phobos/pull/7600
Oh this is great, thank you!
Sep 14 2020