Create array from range

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Oct 10 00:06:03 UTC 2021


On Sat, Oct 09, 2021 at 11:58:14PM +0000, Greg Strong via Digitalmars-d-learn wrote:
> This should be a simple question, but I'm having difficult finding an
> answer.  How do I filter some elements of an array into a new array?
> The filter! function returns a range, but I can't seems to assign it
> to a new array.  I get:
> 
> Cannot implicitly convert expression of type FilterResult!(__lambda10 ...
> 
> Nothing I try to construct a new array seems to work.

You can use the .array function (from std.array) to create an array out
of the filtered range:

--------------
int[] myArray = [ ... ];
int[] filteredArray = myArray.filter!(e => ...).array;
--------------


T

-- 
Chance favours the prepared mind. -- Louis Pasteur


More information about the Digitalmars-d-learn mailing list