Help with Ranges

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 27 16:52:51 UTC 2020


On Sun, Jul 26, 2020 at 07:10:41AM +0000, Charles via Digitalmars-d-learn wrote:
> Suppose I have the following line of code where arr is an array,
> doSomething is some predicate that does a lot of processing on each
> element, sort must come after the mapping, and there are more
> operations done to the range after sort:
> 
> arr.map!doSomething.sort. ...;
[...]

As Steven said, you cannot sort a range that doesn't support swapping
elements, and most ranges cannot unless they're backed by actual
storage, like an array. (*Something* has got to keep track of where the
elements are, after all.)

In this particular case, though, if the contents of your original
doesn't need to be preserved, perhaps .schwartzSort might be what you're
looking for?

If you cannot modify the original array for whatever reason, then an
allocation is probably unavoidable -- either you'll have to create an
array of your mapped elements, or you could create an index and sort
that instead (see .makeIndex or std.range.zip for different approaches).


T

-- 
Those who don't understand Unix are condemned to reinvent it, poorly.


More information about the Digitalmars-d-learn mailing list