Is there a better way to write this split functionality?

Andrej Mitrovic none at none.none
Sun May 8 18:15:58 PDT 2011


import std.stdio;
import std.array;
import std.range;
import std.algorithm;

void main()
{
    auto arr = [64, 64, 64, 32, 31, 16, 32, 33, 64];
 
    auto newarr = arr[];
    bool state = true;
    while (arr.length)
    {
        newarr = state  ? array(until!("a < 32")(arr))
                               : array(until!("a >= 32")(arr));
        arr = arr[newarr.length .. $];
        state ^= 1;
        
        writeln(newarr);
    }
}

The idea is to find as many elements in a sequence that conform to some predicate, followed by as many elements that conform to another predicate. The two predicates are switched on each run.

The above code will print:
[64, 64, 64, 32]
[31, 16]
[32, 33, 64]

Is there a better way to do this, some std.range/algorithm function I don't know of?


More information about the Digitalmars-d-learn mailing list