How for loops, map and comprehensions act on variables?

Preallocating arrays is often a good practice, but you can still pass them as arguments โ€” it doesnโ€™t require you to use globals.

In any case, as @ericphanson pointed out above, a statement like Bโ‚Š = B * M or ๐ƒโ‚Š = [Hermitian(M'*D*M) for D โˆˆ ๐ƒ] allocates new arrays anyway โ€” you are โ€œrebindingโ€ the variables Bโ‚Š and ๐ƒโ‚Š to โ€œpointโ€ to new arrays in memory, not mutating them in-place. See also this discussion.

(This is not unique to Julia! If you do a = [3,4,5], then b = a, followed by a = [4,5,6] in Python or Matlab or most other languages with analogous operations, b will still be [3,4,5].)

1 Like