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

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Feb 23 00:55:02 PST 2013


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?).


More information about the Digitalmars-d mailing list