is it possible to sort a float range ?
Ali Çehreli
acehreli at yahoo.com
Thu Jun 24 01:36:47 UTC 2021
On 6/23/21 6:09 PM, mw wrote:
> I think in most other popular language's std lib:
>
> container.sort();
>
> or
>
> sort(container);
>
>
> is just one call, and the user will expect the `container` is sorted
> after the call.
That's exactly the same in D. What's different is, D's sort() does not
return the original array (more generally, "random access range").
> If D want to convert more people from other more popular languages, it's
> better try to make this conversion as easy as possible.
Agreed but this is not an example of that situation. Instead of calling
release(), one can return the array as well:
import std.algorithm;
lnumRange.sort!(r"a > b"c);
return lnumRange;
I happen to like SortedRange. It's an interesting and useful experiment
that constrains functionality. The problem with other languages, you can
run "binary search" on any array (even if the array is not sorted. The
correctness is left to the programmer, who is proven to be a source of
bugs. Other languages will not complain at all... On the other hand,
Phobos, with the help of SortedRange, protects the programmer from doing
so: You can binary search only on a SortedRange, which is usually a
cheap layer over an existing array or any other kind of RandomAccessRange.
As a system programmer, I appreciate assumeSorted() as well, which
allows me to tell the compiler that "this array is really sorted." So, I
think SortedRange is very useful.
Ali
More information about the Digitalmars-d-learn
mailing list