Transforming a range back to the original type?

Steven Schveighoffer schveiguy at yahoo.com
Fri May 4 13:37:23 PDT 2012


On Fri, 04 May 2012 16:05:25 -0400, Jakob Ovrum <jakobovrum at gmail.com>  
wrote:

> On Friday, 4 May 2012 at 19:17:13 UTC, Steven Schveighoffer wrote:
>> 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
>
> That's not a real example, that's pretty much the same example I  
> provided below the part you quoted.

First, what would you consider a real example?

Second, there's an important piece of the use case that your sample lacks  
-- Jacob is rebinding the result back to the original item.

So for example, I could see code like this:

void displayResults(Container c)
{
   if(maxvalue)
      c = c.filter!(x => x < maxvalue).makeContainer!Container();

   // proceed to display elements from c
}

-Steve


More information about the Digitalmars-d-learn mailing list