Re:‘My Hovercraft Is Full of Eels’ in Many Languages
by simsla on Wednesday May 09, 2018 @09:43AM
(#17029707)
And if a hovercraft is an array of size 331, then it should hold 331 eels.
[ Parent
| on HN ]
Re:‘My Hovercraft Is Full of Eels’ in Many Languages
by peeters on Wednesday May 09, 2018 @10:58AM
(#17030318)
Ah but now we're at the crux of the issue; in Javascript, hovercrafts grow in size to accommodate as many eels as you put in, but to a maximum of ~4 billion. So "full" is not a question you can ask of your hovercraft, though it will tell you once it's overflowing.
Re:‘My Hovercraft Is Full of Eels’ in Many Languages
by Klathmon on Wednesday May 09, 2018 @11:51AM
(#17030779)
Now there's a thought!
What if we used a storage method that didn't grow?
In JavaScript we have typed arrays, we could use a `Uint16Array` to store UTF-16 code points as integers in each element, then we could define the amount of eels that our "hovercraft" could store ahead of time in a way that it won't expand.
const hovercraftEelCapacity = 331 // We can define our hovercraft to have room for 331 eels
const eel = 'eel'
const hovercraft = new Uint16Array(new ArrayBuffer(2 * eel.length * hovercraftEelCapacity)) // Each "eel" takes up 3 16-bit integers
for (let i = 0; i < hovercraftEelCapacity * eel.length; i += eel.length) {
hovercraft[i] = eel.codePointAt(0)
hovercraft[i+1] = eel.codePointAt(1)
hovercraft[i+2] = eel.codePointAt(2)
}
console.log('Your hovercraft full of eels: ', hovercraft)