lua-users home
lua-l archive

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


On Tue, Apr 17, 2012 at 5:41 PM, joao lobato <btnfdp.lobato@gmail.com> wrote:
>
> On 4/17/12, Coda Highland <chighland@gmail.com> wrote:
> > On Tue, Apr 17, 2012 at 3:12 PM, joao lobato <btnfdp.lobato@gmail.com>
> > wrote:
> >> On 4/17/12, Coda Highland <chighland@gmail.com> wrote:
> >>> Okay, yeah, then the only way to verify issequence is to either (1)
> >>> get the real size of the array part (which isn't exposed AFAIK) or (2)
> >>> iterate over pairs(t).
> >>>
> >>> /s/ Adam
> >>>
> >>>
> >>
> >> A sequence can have all the key-value pairs in the array-part, all in
> >> the hash-part or every case in between.
> >>
> >> Those two concepts are orthogonal.
> >>
> >
> > I meant to imply that (1) would be something implemented inside the
> > interpreter because that behavior isn't defined in the spec. (Indeed,
> > the spec doesn't require that anything is implemented array-like as
> > long as Lua scripts can treat it as a sequence.) I realized a while
> > after I sent the message that I didn't make this clear.
> >
> > /s/ Adam
> >
> >
>
> I voiced before my opinion that # could just as well be the number of
> elements in the (hash-)table. That way,
>
>    seq = {1,2,3,4,5}
>    #seq --> 5
>
> #seq would still be the lenght of a sequence or the number of elements
> in a set and would be a O(1) operation. Of course one would lose the
> ability to use the array-side and the hash-side of the same table at
> the same time. That is actually quite practical and trademark Lua.
>
> Petite Abeille is right. It is best to simply master the #op as it stands.
>

Has this idea already appeared before? I don't think it would be
concretely useful, but it could give to the people who complain about
"#" something to chew on while they think...

Suppose that we patch a Lua interpreter slightly to add another field
to the "Table" structure - i.e., to:

  http://www.lua.org/source/5.2/lobject.h.html#Table

Let's call this new field "npik", for the "number of positive integer
keys" in the table. It is relatively cheap to keep it up to date - we
only need to modify the code that implements "T[key] = val". Suppose
that we add a function "npik" that reads the npik field of a table;
then we can implement "issequence" as just the translation of this to
C:

    issequence = function (T)
        for i=1,npik(T) do
          if T[i] == nil then return false end
        end
        return true
      end

  Cheers,
    Eduardo Ochs
    eduardoochs@gmail.com
    http://angg.twu.net/