std.algorithm.splitter on a string not always bidirectional
Jon Degenhardt
jond at noreply.com
Fri Jan 22 05:55:12 UTC 2021
On Friday, 22 January 2021 at 05:51:38 UTC, Jon Degenhardt wrote:
> On Thursday, 21 January 2021 at 22:43:37 UTC, Steven
> Schveighoffer wrote:
>> auto sp1 = "a|b|c".splitter('|');
>>
>> writeln(sp1.back); // ok
>>
>> auto sp2 = "a.b|c".splitter!(v => !isAlphaNum(v));
>>
>> writeln(sp2.back); // error, not bidirectional
>>
>> Why? is it an oversight, or is there a good reason for it?
>>
>> -Steve
>
> I believe the reason is two-fold. First, splitter is lazy.
> Second, the range splitting is defined in the forward
> direction, not the reverse direction. A bidirectional range is
> only supported if it is guaranteed that the splits will occur
> at the same points in the range when run in either direction.
> That's why the single element delimiter is supported. Its
> clearly the case for the predicate function in your example. If
> that's known to be always true then perhaps it would make sense
> to enhance splitter to generate bidirectional results in this
> case.
>
> --Jon
Note that the predicate might use a random number generator to
pick the split points. Even for same sequence of random numbers,
the split points would be different if run from the front than if
run from the back.
More information about the Digitalmars-d-learn
mailing list