Another (maybe better) option is ``` baz!(z,x) = begin ... return z; end baz(x) = baz!(similar(x),x) ``` You can call them by ``` baz!(x,x) z = baz(x) ```
On Saturday, August 22, 2015 at 11:24:51 PM UTC+2, Sisyphuss wrote: > > I don't think it's much less efficient to "copy` in your second example. > In the second function., you should allocate memory for `y` anyway. So why > not give them a initial value especially when the input and the output of > the function are very alike. > > By the way, in some case, you may want to use `deepcopy()` > > 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 >> >