python:
l=[[]]*4; l[0].append("YOLO"); l
> [['YOLO'], ['YOLO'], ['YOLO'], ['YOLO']]Because mutability...
Yeah, actually this post triggered me: https://clojurians.slack.com/archives/C03A6GE8D32/p1710956328696939?thread_ts=1710955729.886189&cid=C03A6GE8D32
I work in a JS codebase as well as a Clojure codebase at work. The JS code uses Immutable.js (and React/Redux) but I still trip over mutability sometimes and I stare at the code and think "WTF?" ... and then I realize "Oh, darn, that modifies the object I'm holding 😞 "
sort on arrays in js mutates the original -- completely unforgivable
that and reverse
Interestingly, it's not what happens in SuperCollider:
(
x = [] ! 4;
x.at(0).add(\WAT);
x;
)
-> [ [ WAT ], [ ], [ ], [ ]
Probably, the [] has an implicit function call in it, so that it's a new instance everytime.Since, for instance
42.rand.dup(4);
-> [ 28, 28, 28, 28 ]
Whereas an explicit function call would be:
{42.rand}.dup(4);
-> [ 38, 25, 37, 25 ]( ! is the same as the .dup method)