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 >>> >>>
