Assigning map result, and in-place map

Pelle pelle.mansson at gmail.com
Fri Sep 3 03:56:19 PDT 2010


On 09/03/2010 12:32 PM, Bert van Leeuwen wrote:
> Pelle Wrote:
>
>> On 09/03/2010 11:42 AM, Bert van Leeuwen wrote:
>>
>>> 2) Related to above, I want to do something like map, but not return a new array, I want to modify elements in-place in the array. How do I do that? (without explicitly iterating with foreach etc.)
>>
>> I don't know if this is intended to be supported, but at least for now
>> this works:
>>
>>       int[] xs = [1,2,3,4,5,6,7];
>>       copy(map!`a*a`(xs), xs);
>>
>>       writeln(xs);
>>
>> [1, 4, 9, 16, 25, 36, 49]
>
>
> Interesting. For large arrays, that seems to take almost double the time of creating a new one with array() (as in Nick's reply). So copy() obviously doesn't do it in place (not surprising given its name), and presumably makes a temporary array first from which to copy to a.
>

Try it again with -inline. For me it's within 10% with inline, and gains 
even more if you add -O and -release.

It also doesn't allocate, which is just a bonus. :-)


More information about the Digitalmars-d mailing list