First of all, in the first 

*julia> ftest = function(x) *
*        x[2] = 0 *
*        return x*
*       end*
*(anonymous function)*

you are modifying the passed object x.

But in the second one you are resetting x.

*ftest2 = function(x) *
*        x = x[[2,1]] *
*        return x*
*end*

I think the optimal solution to this heavily depends on the problem you are 
facing, but generally deepcopy copies fully the object, which means will 
allocate more memory.

Also one weird thing that I came across lately, which is not obvious in the 
light of the previous is that if you take your first function and pass to 
it a row or column of a matrix, the original matrix won't be modified. Try 
for example ftest(eye(2)[:,2]).

Hope this reply helped.

On Sunday, 29 June 2014 11:24:42 UTC+2, Stéphane Laurent wrote:
>
> Hello, 
>
>  As a R user I am a little puzzled by this behaviour:
>
> *julia> ftest = function(x) *
> *        x[2] = 0 *
> *        return x*
> *       end*
> *(anonymous function)*
>
> *julia> y = [1,2]*
> *2-element Array{Int64,1}:*
> * 1*
> * 2*
>
> *julia> ftest(y)*
> *2-element Array{Int64,1}:*
> * 1*
> * 0*
>
> *julia> y*
> *2-element Array{Int64,1}:*
> * 1*
> * 0*
>
>
> In R, this function doesn't modify the variable passed in the argument:
>
> *> f <- function(x){ x[2] <- 0; return(x)}*
> *> y=1:2*
> *> f(y)*
> *[1] 1 0*
> *> y*
> *[1] 1 2*
>
> Please could you tell me whether this is a good solution :
>
> *ftest = function(x) *
> * x = deepcopy(x)*
> * x[2] = 0 *
> * return x*
> *end*
>

Reply via email to