D idom for removing array elements

Jordan Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 29 15:48:40 PST 2017


On Sunday, 29 January 2017 at 23:42:40 UTC, Jordan Wilson wrote:
> On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote:
>> On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote:
>>> [...]
>>
>> I am trying to wrap my head around lazy evaluation during 
>> filtering/mapping, but there's something I don't understand.
>> I want to create an array, square some elements, remove some 
>> elements from original array and add the squared ones to the 
>> original array:
>>
>> [...]
>
> You need to do something like this:
> auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;
>
> It's because arrMap is lazy evaluated.

Specifically:
arr ~= arrMap.array;
will cause arrMap to be evaluated again using whatever arr is.

So instead of:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2);
// mutate arr
arr ~= arrMap.array;

you would want:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;
// mutate arr
arr ~= arrMap;



More information about the Digitalmars-d-learn mailing list