Thread overview
SortedRange.lowerBound from FrontTransversal
Aug 06, 2016
Alex
Aug 08, 2016
Michael Coulombe
Aug 08, 2016
Michael Coulombe
Aug 08, 2016
Alex
August 06, 2016
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...

August 08, 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
August 08, 2016
On Monday, 8 August 2016 at 00:57:41 UTC, Michael Coulombe wrote:
> ...

And looking at the source, the reason it fails when using TransverseOptions.assumeNotJagged is that it does not implement length or $.

I made this into an enhancement request: https://issues.dlang.org/show_bug.cgi?id=16363
August 08, 2016
On Monday, 8 August 2016 at 01:36:43 UTC, Michael Coulombe wrote:
> On Monday, 8 August 2016 at 00:57:41 UTC, Michael Coulombe wrote:
>> ...
>
> And looking at the source, the reason it fails when using TransverseOptions.assumeNotJagged is that it does not implement length or $.
>
> I made this into an enhancement request: https://issues.dlang.org/show_bug.cgi?id=16363

Ah... ok...
Thanks!