lua-users home
lua-l archive

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


Note that the result of #t is also VERY unreliable on tables that contains sequences with holes: it may stop counting prematurely or not, randomly, depending on the number of items with integer keys after the first hole, or the number of non-nil keys before. So #t stops counting when it encounters some holes, but not all (I think this is related to how keys are hashed internally: tables may store integer keys partly in an unhashed array, partly in the hash (even for tablets that were constructed once and never modified), because the two parts are not balanced to choose which part will store the value associated to the integer key.

If something must be deprecated it is #t (except when t is a string), not ipairs(t) which is more reliable as it can effectively enumerate all integer keys (in a sequence or not), but note also that ipairs(t) on tables is not warrantied to return all keys in ascending order (this would require collecting all keys into an internal sequence table kept in the iterator, then sort that internal sequence of keys, before possibly compacting the successive ranges into another sequence table containing the list of ranges that will be counted one by one by the iterator's next() operation...)

What is intriguating is that pairs() and ipairs() have no parameter to indicate that we want a specific order or not for the enumeration. It should not need to sort them by default, unless we provide a function to compare keys and sort them.





Le lun. 25 mai 2020 à 22:41, Philippe Verdy <verdyp@gmail.com> a écrit :
I also agree: #t is costly on sequences as they need first to be fully enumerated (and some sequences created from an input stream cannot be enumerated several times, it would cost too much to retain the full sequence in a cache for allowing it to be enumerated again for the for loop).
For tables and strings however, #t is instantaneous and ipairs() not needed... except when tables are not sequences and have integer keys outside the consecutive range 1 to N, that ipairs() could still enumerate all (ipairs can simply do the same as pairs(), filtering just keys according to their type, assuming that pairs() return all integer keys in ascending order.



Le jeu. 21 mai 2020 à 19:26, Andrew Gierth <andrew@tao11.riddles.org.uk> a écrit :
>>>>> "Coda" == Coda Highland <chighland@gmail.com> writes:

 >> why the _ipairs metamethod was deprecated?

 Coda> Because there's no need for a metamethod for something as simple
 Coda> as ipairs()

Some of us greatly disagree with this position - in particular, it
seriously impacts what you can do with userdatas.

For example, if embedding into an environment (e.g. a database) which
has a native array type (such as SQL arrays) which can be iterated
sequentially more efficiently than they can be indexed into (because
they have variable-length elements), it means that you can't just
provide an __ipairs and have everything just work in the obvious way;
instead, ipairs() will work inefficiently and you have to provide a new
but functionally identical method.

--
Andrew.