lua-users home
lua-l archive

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




On Tue, May 7, 2019 at 1:55 PM Coda Highland <chighland@gmail.com> wrote:
On Tue, May 7, 2019 at 1:12 PM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
In my opinion the "length" of a number can only be the length of the string generated when converting this number to a string with the default string conversion.
Note that keys in tables are not necessarily integers, they can be any number. It is an integer only to restrict tables to their sequence, but not all tables have a unique sequence, if there are "holes" such that there exists a integer key k in table t, where k >= 2, t[k] is not null, and t[k-1] is null).
Codes that expect sequences to give a meaning to "#t" cannot be portable if t is not a sequence with no holes. But there's no easy builtin properties of tables that can assert it forms a unique predictable sequence (note that the presence of non-integer keys in the table does not invalidate a sequence: a table is a valid sequence depending only on the subset of its keys that are integers, and keys below 1 are ignored: you can safely add a key 0 or -1 to any table that is a sequence without invalidating the sequence and what #t will return).
This behavior of tables in Lua and the very weak definition of "sequences" is very tricky.

Except that #s being the length of a string makes sense, because a string is a sequence of characters. But if numbers are considered to be scalar values (as they usually are) then that means they aren't sequences, and the Lua definition of # being the length of a sequence is meaningless.

That said, if you step outside of the concrete implementation of numbers in computers, you could use the set-theoretic definition of the natural numbers to come up with another interpretation of #n -- which would be n itself. Under the Zermelo-Fraenkel axioms, the natural numbers are built up of sets. 0 is defined as the empty set, and then every natural number afterward is defined as the set of all natural numbers smaller than it. This means the number of elements in the set representing a natural number IS that natural number, and since natural numbers have a defined ordering, that means that a natural number is a sequence.

What of negative numbers and non-integer numbers? Well, if we consider a sequence to be something iterable, and the length of a sequence to be the number of elements inspected when iterating over it, then #r would be floor(r) for positive non-integer numbers, as the default notion of iterating over the numbers involves incrementing by 1 and stopping once you've exceeded that number. (It WOULD be ceil, since Lua includes the upper loop bound, except Lua is also 1-indexed, which means that the iteration count ends up being the same.) #r would be 0 for negative numbers (whether integer or not) as a for loop with 1 as its lower bound and with a negative number as its upper bound will iterate 0 times.

So #n = (n > 0 and floor(n) or 0) is the only definition of # on a number that I can think of that would be consistent with the other sequence-related uses of the # operator in Lua.

/s/ Adam

It occurs to me that #n for integer n >= 2^64 or non-integer n > 2^53 would need to be inf, if we say that # returns the number of iterations a loop over its argument would perform.

/s/ Adam