5 ms·
I have an example of a similar journey I followed. The Lua language has a 1-based indexing scheme. Furthermore, the only thing that differentiates a hash-map f
by eblume 11y ago
I have an example of a similar journey I followed.
The Lua language has a 1-based indexing scheme. Furthermore, the only thing that differentiates a hash-map from an ordered array is the existence of only keys in the range [1, ∞) (and they must be entirely contiguous starting from 1 until the last present key).
The interface for arrays and hash-map data types is uniform, called a 'table', which has your standard `mapper[key] = value` syntax (and also, equivalently, `mapper.key = value` for well-formatted string-literal keys).
This results in the very surprising and confounding fact that if a naive programmer doesn't realize that Lua has this system, they will very likely end up starting from 0 and build an unordered hash map rather than an ordered array - complete with all the resulting performance penalties and unordered traversals.
For a long time this struck me as being absolutely absurd and very damning. After working with Lua for a while, though, there began to be a certain elegance to it that I could admire. There is a SO post that goes in to some of the benefits[0][1].
I still much prefer 0-based indexing though.
0: http://stackoverflow.com/questions/2785704/why-do-lua-arraystables-start-at-1-instead-of-0 http://stackoverflow.com/questions/2785704/why-do-lua-arrays...
1: Irony intended.