[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: table.maxn...
- From: Coda Highland <chighland@...>
- Date: Mon, 1 Jun 2015 08:12:45 -0700
On Mon, Jun 1, 2015 at 8:05 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2015-06-01 16:24 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>>> On Mon, Jun 1, 2015 at 6:26 AM, Coda Highland <chighland@gmail.com> wrote:
>>> > On Mon, Jun 1, 2015 at 3:06 AM, Tim Hill <drtimhill@gmail.com> wrote:
>>> >> This also takes care
>>> >> of the issue of FP errors, since all the math is now done on integers.
>>>
>>> Also, it doesn't resolve FP errors in Lua 5.1/5.2, because numbers are
>>> still double-precision floats and not true integers. The act of adding
>>> 1 + 1<<53 is what causes the problem, not dealing with fractional
>>> values.
>>
>> I use the following code (or rather I would use it if ever I had to
>> check whether a table is a sequence :-):
>>
>> function issequence (t)
>> local n = #t
>> local c = 0 -- count keys in [1..n]
>> for k in pairs(t) do
>> if type(k) == 'number' and k > 0 then
>> if k ~= math.floor(k) or k > n then return false end
>> c = c + 1
>> end
>> end
>> return c == n
>> end
>
> The right question is not whether a table is a sequence, but whether
> the default #tbl is deterministic for the particular table, i.e. whether the
> table has no holes. Being a sequence is merely a sufficient condition. The
> iterations `for i=1,#tbl` and `for i in ipairs(tbl)` both ignore
> non-integer keys.
>
> So I would be tempted to change the function name to "hasnoholes" and
> the loop body to
>
> if type(k) == 'number' and k > 0 and k==math.floor(k) then
> if k > n then return false else c=c+1 end
> end
In other words, what my first attempt at a predicate did. :P
I do think that this is a more useful predicate than simply "is a
sequence" since very little actually cares about the strict definition
of a sequence.
/s/ Adam