Transforming a range back to the original type?

Jonathan M Davis jmdavisProg at gmx.com
Fri May 4 15:24:39 PDT 2012


On Friday, May 04, 2012 16:37:23 Steven Schveighoffer wrote:
> 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
> }

std.container has make, which should do essentially that. And since you have 
the original container, you know what its type is, so there's no need to query 
the range for the original container type. Something like this should work:

c = make!Container(filter!(x => x < maxvalue)(c));

If it doesn't, then make and/or the container needs to be improved so that it 
does.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list