Filter and Map's weird return types cause frustration...

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Feb 23 01:44:57 PST 2013


On 2/23/13 10:55 AM, Chris Nicholson-Sauls wrote:
> Straight from the documentation: "returns a new range" -- no guarantee
> made about the relation of return type and parameter type.
>
> These functions return range objects to amortize the potential cost of
> the operation, with a very small initial cost (a reference to the input
> range), which is actually very useful in a great many situations. They
> are all written as generics operating on ranges as both input and
> output. For better or worse, this has become the de facto D idiom. If
> you really do just want the array out of it, import std.array and call
> the eponymous function:
>
> example = example.filter!isLongEnough().array();
>
> Voila. This iterates the full range and collects the results, as
> expected. What *would* be nice would be to have "InPlace" variations of
> these functions for use cases such as yours, in order to re-use resources.
>
> example.filterInPlace!isLongEnough();
>
> Bam, done; assuming the input is a random access range (which a basic
> array is, is it not?).

example = example.remove!isTooShort;

Andrei


More information about the Digitalmars-d mailing list