Yes, this is a common way to do this. Usually to get the non-mutating behavior 
you need to make a copy first anyway. (Some languages just do so implicitly and 
don't allow anything else.) Sometimes, as Kristoffer mentions, there might be a 
way to construct a new object directly instead of copying and mutating.

> On Aug 22, 2015, at 4:21 PM, Kristoffer Carlsson <kcarlsso...@gmail.com> 
> wrote:
> 
> What do you mean copying the object is inefficient? Do you mean that it is 
> cheaper to build it from scratch than to start with the copy?
> 
>> On Saturday, August 22, 2015 at 9:51:48 PM UTC+2, Timothée Poisot wrote:
>> Hi, 
>> 
>> I caught myself wondering about the "correct" way to use function and 
>> function! -- or rather, how other people deal with this. 
>> 
>> Let's say I have a simple function that operates on an array, and I 
>> want a version to modify the original object, and one that doesn't. 
>> 
>> Is this the correct way of doing it? 
>> 
>> ~~~ 
>> function baz!(x) 
>>         # Do things on x 
>> end 
>> 
>> function baz(x) 
>>         y = copy(x) 
>>         baz!(y) 
>> end 
>> ~~~ 
>> 
>> This allows to reuse the code of baz!, but copying the object IS 
>> inefficient. How do you usually deal with this situation? 
>> 
>> t 

Reply via email to