other-languages

grav 2024-03-21T20:12:47.442309Z

python:

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

🫠 2
seancorfield 2024-03-21T20:14:26.296859Z

Because mutability...

seancorfield 2024-03-21T20:26:09.225299Z

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

Cora (she/her) 2024-03-22T04:55:54.492669Z

sort on arrays in js mutates the original -- completely unforgivable

Cora (she/her) 2024-03-22T04:57:35.686519Z

that and reverse

grav 2024-03-23T21:23:19.463559Z

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.

grav 2024-03-23T21:23:43.322659Z

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 ]

grav 2024-03-23T21:27:32.247729Z

( ! is the same as the .dup method)