Exposing pred from SortedRange and changes to algorithms that assumeSorted

Seb seb at wilzba.ch
Fri Jan 12 10:53:04 UTC 2018


On Friday, 12 January 2018 at 09:52:36 UTC, aliak wrote:
> Would it be an acceptable enhancement to phobos to expose the 
> predicate in SortedRange 
> (https://dlang.org/library/std/range/sorted_range.html)?
>
> The rationale behind it would be so that functions like 
> setDifference 
> (https://dlang.org/library/std/algorithm/setops/set_difference.html) or any function that requires a sorted range to "run" does not need to be provided the sorting predicate, ie:
>
> setDifference
> largestPartialIntersection
> largestPartialIntersectionWeighted
> multiwayUnion
> setIntersection
> setSymmetricDifference
> setUnion
> multiwayMerge
>
> Where functions do required ranges to be sorted and a predicate 
> passed in, this enhancement should reduce programmer errors 
> (i.e. providing the wrong predicate to the algorithm that was 
> used to sort the range that you want to operate on). The 
> sortedrange knows how it was sorted, so there should be no need 
> to duplicate that (unless there are maybe some valid reasons?).
>
> It would also make for SortedRange itself to be made lighter 
> (maybe not a good idea at this point but it can be done at 
> least) by moving it's functionality in to algorithms that are 
> already there. Eg, "SortedRange.contains" can be done by 
> algorithm.canFind:
>
> auto canFind(Range, T)(Range range, T element) if 
> (isSortedRange!Range) {
>   // do binary search for element
> }

canFind uses find internally, which already has a shortcut for 
SortedRange.
I don't like contains either, but the idea was to have a separate 
method with different performance guarantees as canFind is 
typically O(n).
Anyways I have tried to deprecate it:

https://github.com/dlang/phobos/pull/5651

Maybe you have better arguments?

----

Now to your main question:

Exposing doesn't help much, because you can't compare them, but 
there is WIP on lambda comparisons:

https://github.com/dlang/dmd/pull/7484

With it, the default lamdas can be compared for equality and what 
you want to do can/will be done.

> PS: Is this the correct place for posts like this? Apologies if 
> not :s

Yeah I think it is.


More information about the Digitalmars-d mailing list