Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Stefan Karpinski
Whether this is surprising to you or not depends entirely what language you are coming from. For Matlab and R users, this is a significant difference. For people coming from C, C++, Fortran, Python, Ruby, Perl, Java, and Lisp, this is unsurprising. You can easily implement the non-mutating behavior

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Tomas Lycken
The problem lies in detecting (or, rather, teaching the compiler to detect) if a function - or any of the functions it calls - mutates an argument; this is a fairly difficult problem. If you take a look at the Julia standard library, you'll see that a lot of functions have both mutating and non

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Páll Haraldsson
I wander if the convention should have been made the other way as not all will know it at first.. If you do not want your arguments touched then you can trivially make a non-mutating wrapper using the similar function. It seems that could have been automated.. Maybe you can with a macro? Woul

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Tomas Lycken
Glad to be of service! =) Languages treat both aliasing and argument passing differently, so it really just takes a little while to experiment and see how this language plays out what you're trying to do. For what it's worth, Julia also has a style convention to end all method names with a bang

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Bradley Setzler
Thanks Tomas, the similar command will be very useful in avoiding this issue. Thank you also for a thoughtful and informative response, Tomas. Bradley PS - For what it's worth, R does not have this behavior. On Friday, December 26, 2014 2:05:44 PM UTC-6, Tomas Lycken wrote: > > The main rea

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Tomas Lycken
The main reason is performance; passing and aliasing arrays this way (those are two different concepts, which work together to make this particular example a little confusing) allow for writing code that is as fast as possible, by leaving the coder in control of when a copy is made and when it

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Bradley Setzler
Why would you want this behavior? How could you possibly benefit from modifying X anytime you modify Y just because Y=X initially? If I wanted to modify X, I would modify X itself, not Y. Bradley On Friday, December 26, 2014 1:53:12 PM UTC-6, John Myles White wrote: > > This is aliasing. Al

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Isaiah Norton
Julia passes arrays by reference and always has: http://julia.readthedocs.org/en/latest/manual/arrays/#multi-dimensional-arrays On Fri, Dec 26, 2014 at 2:49 PM, Bradley Setzler wrote: > Hi, > > I cannot explain this behavior. I apply a function to a variable in the > workspace, the function init

Re: [julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread John Myles White
This is aliasing. Almost all languages allow this. -- John Sent from my iPhone > On Dec 26, 2014, at 2:49 PM, Bradley Setzler > wrote: > > Hi, > > I cannot explain this behavior. I apply a function to a variable in the > workspace, the function initializes its local variable at the workspa

[julia-users] Julia modifies variable outside of function when operating on different variable inside of function

2014-12-26 Thread Bradley Setzler
Hi, I cannot explain this behavior. I apply a function to a variable in the workspace, the function initializes its local variable at the workspace variable, then modifies the local variable and produces the desired output. However, it turns out the Julia modifies both the local and workspace