Fork me on GitHub
#other-languages
<
2024-03-21
>
grav20:03:47

python:

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

2
seancorfield20:03:26

Because mutability...

seancorfield20:03:09

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)04:03:54

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

Cora (she/her)04:03:35

that and reverse

grav21:03:19

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.

grav21:03:43

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 ]

grav21:03:32

( ! is the same as the .dup method)