On Monday, 27 July 2020 at 16:52:51 UTC, H. S. Teoh wrote:
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.)

But in my example, isn't the output of map backed by actual storage? After all, it's taking in an array.

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

I'll take a look at both of these, since I need to be aware of both cases. I'm trying to find the most efficient way of building a pipeline for my own betterment. Thank you.

Reply via email to