How to filter an array so the result is an array again?

Cheng Wei rivercheng at gmail.com
Thu Sep 15 21:04:39 PDT 2011


The standard library std.algorithm is based on Range. So if
a = [1, 2, 3, 4];
auto r = filter!("a < 2")(a);

Here, r is a range.
How about I want an new array? Is there any easy way to convert the
result to array?

If we have to do like:
int[] b;
for (v; r) {
    b ~= v;
}

Then maybe it is easier to not use filter at all as:
int [] b;
for (v; a) {
   if (v < 2) b ~= v;
}

Thanks a lot.


More information about the Digitalmars-d-learn mailing list