Help with Ranges

Charles charles at email.sometld
Mon Jul 27 17:10:44 UTC 2020


On Sunday, 26 July 2020 at 14:56:35 UTC, Steven Schveighoffer 
wrote:
> A map that returns an lvalue would be sortable, but you would 
> be sorting the processed elements, and probably not the 
> original elements.

Indeed, but that's what I want: sort the process elements. 
Otherwise, I'd place sort before the transformations.

> I have found this handy tool quite useful in my code where I 
> need a temporary array:
>
> // creates a concrete range (std.container.array.Array range) 
> out of the
> // original range that is eagerly fetched, and then can be 
> processed, without
> // allocating extra garbage on the heap.
> auto concreteRange(Range)(Range r)
> {
>     import std.range : ElementType;
>     import std.container.array : Array;
>     return Array!(ElementType!Range)(r)[];
> }
>
> Slicing an Array will keep the reference count correctly, and 
> destroy the memory automatically after you're done using it. So 
> it's perfect for temporary arrays in pipelining.
>
> -Steve

This works well, and it's rather nifty. Still, I'm confused 
since, as far as I know, map wraps its source, i.e. the array in 
this case, which is sortable. It seems to me the only reason I 
can't sort MapResult is because it doesn't have the proper 
interface.

I'm obviously missing something.


More information about the Digitalmars-d-learn mailing list