I may biased by years of Numpy usage, but I would find it very intuitive that x .+= y change x in place.
On Thursday, May 1, 2014 10:34:41 AM UTC-7, Stefan Karpinski wrote: > > It occurs to me that now that we separate .+ from + it may make sense for > x .+= y to be allowed to mutate x. After all, you are saying that you > expect an array in some sense. > > On May 1, 2014, at 1:25 PM, Patrick O'Leary > <[email protected]<javascript:>> > wrote: > > A longish discussion of pros, cons, and other things can be found in this > issue: > > https://github.com/JuliaLang/julia/issues/249 > > On Thursday, May 1, 2014 11:39:14 AM UTC-5, Dominique Orban wrote: >> >> I would have expected b .+= 5 to change b in place, but it doesn't seem >> to be the case. Isn't it counter-intuitive that it would also make a copy? >> >> On Thursday, May 1, 2014 6:54:10 AM UTC-7, Freddy Chua wrote: >>> >>> do this >>> >>> b = [1:5] >>> f(x) = x + 5 >>> map!(f, b) >>> >>> On Thursday, May 1, 2014 9:03:35 PM UTC+8, Kevin Squire wrote: >>>> >>>> b[:] = b .+ 5 >>>> >>>> has the behavior that you want. However, it creates a copy, does the >>>> addition, then copies the result back into b. >>>> >>>> So, looping (aka devectorizing) would generally be faster. For simple >>>> expressions like these, though, the Devectorize.jl package should allow >>>> you >>>> to write >>>> >>>> @devec b[:] = b .+ 5 >>>> >>>> It then rewrites the expression as a loop. It isn't able to recognize >>>> some expressions, though (especially complex ones), so YMMV. >>>> >>>> (Actually, it may not work with ".+", since that is a relatively new >>>> change in the language. If you check and it doesn't, try submitting a >>>> github issue, or just report back here.) >>>> >>>> Cheers! Kevin >>>> >>>> On Thursday, May 1, 2014, Kaj Wiik <[email protected]> wrote: >>>> >>>>> OK, thanks, makes sense. But how to change the original instance, is >>>>> looping the only way? >>>>> >>>>> On Thursday, May 1, 2014 3:12:51 PM UTC+3, Freddy Chua wrote: >>>>> >>>>> b = b .+ 5 >>>>> >>>>> creates a new instance of an array, so the original array pointed to >>>>> by "b" is not changed at all. >>>>> >>>>> >>>>> >>>>> On Thursday, May 1, 2014 7:39:14 PM UTC+8, Kaj Wiik wrote: >>>>> >>>>> As a new user I was surprised that even if you change the value of >>>>> function arguments (inside the function) the changes are not always >>>>> visible >>>>> outside but in some cases they are. >>>>> >>>>> Here's an example: >>>>> >>>>> function vappu!(a,b) >>>>> a[3]=100 >>>>> b = b .+ 5 >>>>> (a,b) >>>>> end >>>>> >>>>> c = [1:5] >>>>> d = [1:5] >>>>> >>>>> vappu!(c,d) >>>>> ([1,2,100,4,5],[6,7,8,9,10]) >>>>> >>>>> c >>>>> 5-element Array{Int64,1}: >>>>> 1 >>>>> 2 >>>>> 100 >>>>> 4 >>>>> 5 >>>>> d >>>>> 5-element Array{Int64,1}: >>>>> 1 >>>>> 2 >>>>> 3 >>>>> 4 >>>>> 5 >>>>> >>>>>
