Transforming a range back to the original type?

Steven Schveighoffer schveiguy at yahoo.com
Fri May 4 12:17:13 PDT 2012


On Fri, 04 May 2012 12:32:39 -0400, Jakob Ovrum <jakobovrum at gmail.com>  
wrote:

> On Friday, 4 May 2012 at 15:47:31 UTC, Steven Schveighoffer wrote:
>> I think the use case is, instead of defining some transformation  
>> function X as a member of a container, you:
>>
>> 1. define X as a function that accepts a range and returns a range
>> 2. define a way to obtain a range of all elements from a container
>> 3. define a way to construct a new container from a range.
>>
>> Then you only have to define X once, instead of on every container  
>> type.  And you can specialize some containers for X because of UFCS.   
>> See Jacob's example.
>
> After a quick look over the thread again, I still don't see any real  
> examples of use cases from Jacob (I admit I could still be missing it...  
> somewhere...).

This one:

Collection c = new Collection();
c = c.filter!(x => x < 3).toCollection();

filter isn't a property of c, it's a range-producing function.  So I only  
have to define filter once, as a range accepting, range producing  
function.  And any container type, as long as it can produce a range, can  
use this as a pseudo method (via UFCS) to make a filtered copy of itself.

-Steve


More information about the Digitalmars-d-learn mailing list