Is there a better way to write this split functionality?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon May 9 21:18:45 PDT 2011


Those could be nice solutions, thanks.

My first code needlessly resizes the original array though. I could
simply track the lower index instead:

void main()
{
    dchar[] arr = [64, 64, 64, 32, 31, 16, 32, 33, 64];
    dchar[] newarr;
    size_t index;

    bool state = true;
    while (index < arr.length)
    {
        newarr = state ? array(until!("a < 32")(arr[index..$]))
                              : array(until!("a >= 32")(arr[index..$]));
        index += newarr.length;
        state ^= 1;

        writeln(cast(int[])newarr);
    }
}

You know what sucks? I can't assign a range with different predicates
to the same variable. E.g. this won't compile:
       auto newarr = state ? (until!("a < 32")(arr[index..$]))
                                    : (until!("a >= 32")(arr[index..$]));

Error: incompatible types for
((until(arr[index..__dollar],cast(OpenRight)1)) ?
(until(arr[index..__dollar],cast(OpenRight)1))):
'Until!(pred,dchar[],void)' and 'Until!(pred,dchar[],void)'

I mean they are basically the same range type, with only a different
predicate. Why every template instantiation has to be its own unique
type, I'll never understand.


More information about the Digitalmars-d-learn mailing list