Thank you all for the ideas! Leaning towards replace
and replaceAt
.
Also considered setElements
and setElementsAt
. Felt too verbose, too much camelCase (oh how far I’ve come from my days coding enterprise Java). C++ also has assign
- but this also implies updating size, and emplace
which inserts/adds. Thesaurususing a little I found pave
which I find hilarious but would be perfect otherwise.
BTW - length comes from the array’s size, and literals size are pre-calculated during compile. The whole array is allocated at once, and then paved bit by bit as the expressions within the literal are resolved.
@Scruffynerf, you asked about copying arrays, this isn’t too bad with existing tools:
Array copy:
arrayMapTo(src, dest, v -> v)
Partial copy e.g. starting at index 12 in src
for the size of dest
:
dest.mutate((v, i) -> src[i+12])
(src must be at least 12 elements larger than dest in this example)
About .read
that would be slice()
(docs). Also in ES6 you can consume arrays using ES6’s spread syntax as arguments or in array literals.
Continued ramblings about JS follow…
Ideally PB code could be used in JS environments with a little bit of polyfill. Strings aren’t arrays even if you can access characters using square brackets, and arrays don’t have a replace function in JS/ES6. Strings are array-like enough that you can run many of Array’s prototype functions on them though. E.g. to convert a string to an array, you can call Array’s slice()
on it, setting this
to the string, as if it were a member method of the string:
Array.prototype.slice.apply("123") // gives [ '1', '2', '3' ]
(String has it’s own slice
that returns strings)
Or if you are brave, dumb, or smart enough, you can install it into String’s prototype, which is one of the coolest and most dangerous JavaScripty things you can do.
"abc".slice(1) // gives "bc"
String.prototype.sliceAsArray = Array.prototype.slice
"abc".sliceAsArray(1) // gives [ 'b', 'c' ]
Thats all to say that it would be possible to polyfill replace
for JS code that works on arrays and doesn’t break String’s replace
.