[Issue 11415] Assign range to array
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Nov 2 02:44:22 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11415
monarchdodra at gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
CC| |monarchdodra at gmail.com
Resolution| |INVALID
--- Comment #3 from monarchdodra at gmail.com 2013-11-02 02:44:19 PDT ---
(In reply to comment #2)
> Filter produces a lazy range. In other words, it is not an int array. To store
> the result of filter in d[] you need to eagerly evaluate it. To do that you can
> use std.array.array on the result of filter.
> The code becomes:
>
> int[] d = [1,2,3,4,5,6,7];
> d = d.filter!(x => x > 3).array();
>
> If this answers your question, please close the bug.
>
> http://dlang.org/phobos/std_array.html#.array
Yes. Do note though that this will allocate a new array, and not *copy* filter
"into" the "d" array. Not that it's wrong, I just want to highlight it, as I
don't think its quite what the op wanted.
If you want to assign the *contents*, then std.algorithm.copy will do what you
want:
http://dlang.org/phobos/std_algorithm.html#copy
int[] d = [1,2,3,4,5,6,7];
d.filter!(x => x > 3)().copy(d);
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list