other-languages 2024-03-21

python:

l=[[]]*4; l[0].append("YOLO"); l
> [['YOLO'], ['YOLO'], ['YOLO'], ['YOLO']]

🫠 2

Because mutability...

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)