The return of std.algorithm.find

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 15 01:53:06 PST 2016


On 11/15/2016 01:50 AM, drug wrote:
> 15.11.2016 12:48, drug пишет:
>> 15.11.2016 12:43, RazvanN пишет:
>>> The find function which receives an input haystack and a needle returns
>>> the haystack advanced to the first occurrence of the needle. For normal
>>> ranges this is fine, but for
>>> sorted ranges (aka SortedRange) it is a bit odd. For example:
>>>
>>> find(assumeSorted[1, 2, 4, 5, 6, 7], 4) would return [4, 5, 6, 7]. This
>>> is in terms with the general policy of the find function, but is weird.
>>> Since we know the range is sorted, shouldn't the result be [1, 2, 4]?
>>>
>>>
>>>
>> I don't think so. You could use findSplit, it returns three ranges [1,
>> 2], [4], [5, 6, 7].
>
> See also SortedRange.trisect

Indeed. This is what I was typing:

import std.stdio;
import std.range;

void main() {
     auto r = assumeSorted([1, 2, 4, 5, 6, 7]);
     auto found = r.trisect(4);

     auto desired = chain(found[0], found[1]);
     writeln(desired);
}

Prints

[1, 2, 4]

Ali



More information about the Digitalmars-d-learn mailing list