lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Tuesday, June 9, 2015, "书呆彭, Peng Yi" <nerditation@outlook.com> wrote:
在 2015/6/6 12:33, Tim Hill 写道:

My view of this is simple; a sequence is the Lua equivalent of an array (or as near as it gets to one). Other languages have survived for decades only allowing positive integral indices in arrays, why is it a problem in Lua?

If you have non-integral numeric keys, you don’t have an array; you have a dictionary with arbitrary keys that happen to be numeric. The # operator is pretty meaningless in this case.

If you really have need for a set of data items indexed by integer keys (an array), and an additional set of *different* data items with non-integral keys, imho you are better off keeping these in discrete tables (perhaps inside a top-level table); segregating data by clever tricks with the data domains is always messy in my experience.

—Tim


I was sometimes thinking maybe we've made it too explicit that
a table in Lua had an __array part__.

the table type in Lua serves two distinguashed purposes:
 1) as a compound data type, like struct or record in other languages
 2) as a container data type, like array or list in other languages

many are told that a table has an array part and a hash part. thus many
tend to mix compound data and container when they are thinking in Lua.


-- the nerdy Peng / 书呆彭 / Sent from Thunderbird



I think that the mistake happens when people start designing extensions around it or otherwise wish to rarify it and turn it into part of the spec... Like I am here...

However, tables have two features that work if you have a sequence: the length operator will return the last integral index and ipairs will iterate, *in order* from 1 to max n. However, ipairs is merely duplicating `for i=1, t# do`, so it isn't critical, and length could be written in Lua, save for the token. So I guess I see your point..... Or maybe the length operator is more to blame?

As far as red black trees: that works if the tree is holding structures that are native in C, but I'm not sure how you'd implement it in C and have it work in a table. 

Even for me, this isn't something that I can know if it's a problem or not, until the library is used. IRL: there may be very few insertions in very small tables. If it gets bad, I'll get fancy. 

-Andrew