SortedRange.lowerBound from FrontTransversal

Michael Coulombe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 7 17:57:41 PDT 2016


On Saturday, 6 August 2016 at 23:00:42 UTC, Alex wrote:
> Hi all... a technical question from my side...
> why the last line of the following gives the error?
>
> import std.stdio;
> import std.range;
> import std.algorithm;
>
> void main()
> {
>     size_t[][] darr;
>     darr.length = 2;
>     darr[0] = [0, 1, 2, 3];
>     darr[1] = [4, 5, 6];
>     auto fT = frontTransversal(darr);
>     assert(equal(fT, [ 0, 4 ][]));
>
>     auto heads = assumeSorted!"a <= b"(fT);
>     writeln(heads.lowerBound(3)); //!(SearchPolicy.gallop)
> }
>
> The error is:
> Error: template 
> std.range.SortedRange!(FrontTransversal!(ulong[][], 
> cast(TransverseOptions)0), "a <= b").SortedRange.lowerBound 
> cannot deduce function from argument types !()(int), candidates 
> are:
> package.d(7807,10): 
> std.range.SortedRange!(FrontTransversal!(ulong[][], 
> cast(TransverseOptions)0), "a <= 
> b").SortedRange.lowerBound(SearchPolicy sp = 
> SearchPolicy.binarySearch, V)(V value) if 
> (isTwoWayCompatible!(predFun, ElementType!Range, V) && 
> hasSlicing!Range)
>
> I tried also with "assumeNotJagged" for the FrontTransversal, 
> it didn't worked either, beyond the fact, that assumeNotJagged 
> is not of interest for me...

Unfortunately, frontTraversal is not giving you a random access 
range, only a bidirectional range, which means it does not 
support indexing or slicing. It appears that the TraversalOptions 
doesn't cover the case where each range is "long enough" to be 
indexed but not equal length.

static assert(isBidirectionalRange!(typeof(fT))); // succeeds
static assert(isRandomAccessRange!(typeof(fT)));  // fails
static assert(__traits(compiles, fT[0]));         // fails
static assert(__traits(compiles, fT[0 .. 2]));    // fails

In the mean time, you can use this simple alternative:

auto fT = darr.map!front; // for arrays
auto fT = darr.map!"a.front"; // for any range


More information about the Digitalmars-d-learn mailing list