lua-users home
lua-l archive

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




On Tue, Apr 3, 2018 at 7:27 AM, Soni "They/Them" L. <fakedme@gmail.com> wrote:

What about 'rawlen' and the C function 'lua_rawlen'? They ignore the __len metamethod.

--
--


What about a table.border that respects __index?

There are two ways to look at a table in Lua:

- through the usual array index operations, and through the length operator, which allows you to override standard behaviors.
- through the 'raw...' set of functions which don't.

There's nothing stopping anyone from implementing 'border' in C or Lua, so I don't see the point of adding random permutations of raw and 'cooked' access to tables.
Your table.border that respects __index looks something like this:

local function border(t)
    local hi = 1
    while t[hi] ~= nil do
        hi = hi * 2
    end
    --  invariant: (lo == 0 or t[lo] ~= nil) and t[hi] == nil
    local lo = 0
    while lo + 1 < hi do
        local m = lo + (hi - lo) // 2
        if t[m] ~= nil then
            lo = m
        else
            hi = m
        end
    end
    return lo
end


--
--