R.filter!(..).sort!(..)

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 28 13:24:09 UTC 2017


On 11/28/17 8:10 AM, Arjan wrote:
> When applying a sort!() on a filtered range I get this compiler error:
> 
> Error: template std.algorithm.sorting.sort cannot deduce function from 
> argument types !((a, b) => a.name < b.name)(FilterResult!(__lambda3, 
> RangeT!(Array!(IssueType)))), candidates are:
> C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(1851,1):        
> std.algorithm.sorting.sort(alias less = "a < b", SwapStrategy ss = 
> SwapStrategy.unstable, Range)(Range r) if ((ss == SwapStrategy.unstable 
> && (hasSwappableElements!Range || hasAssignableElements!Range) || ss != 
> SwapStrategy.unstable && hasAssignableElements!Range) && 
> isRandomAccessRange!Range && hasSlicing!Range && hasLength!Range)
> 
> But it seems the problem is with the filter!() result not being a 
> isRandomAccessRange!Range because:
> R.filter!(..).array.sort!(..) just works (by copying the filter results 
> in a array).
> 
> Iaw is the compiler error msg wrong? Or i'm I wrong?

The library is correctly telling you that your filtered range is not 
random access. It can't be, because it lazily applies the filter (that 
is, it filters on each element as you popFront them). So how can it know 
what the e.g. 3rd element is, if you haven't run any filters yet?

The array version works because you are applying the filter completely 
and storing the results elsewhere in one step.

-Steve


More information about the Digitalmars-d-learn mailing list