lua-users home
lua-l archive

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


On Fri, Dec 3, 2010 at 3:53 PM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> On Fri, Dec 3, 2010 at 12:39 PM, Hisham <hisham.hm@gmail.com> wrote:
>> >From those uses, I see two sensible deterministic definitions for #,
>> which would be either:
>>
>> * the number of elements in the table
>
> besides the "if #t == 0" (note we already have "if next(t)==nil"), i
> don't see where this can be useful.  IOW: deterministic but useless

Having had to explain the behavior of # to a number of people who
otherwise 'got' the rest of the language just fine (and looking at
their stares and saying "yeah, I know") I would find the simplicity
and determinism to be a big plus. :) And I find size to be a useful
property in data structures other than arrays. In LuaRocks, for
example, I have code like this:

   local first = next(results)
   if not first then
      return nil, "No results matching query were found."
   elseif first and next(results, first) == nil then
      return pick_latest_version(query.name, results[first])
   else
      return results
   end

which would arguably be more readable with "if #results == 0" and
"elseif #results == 1"... But I can see how one of the goals of # was
to allow t[#t+1]=x (and table.insert) in tables that have both array
and hash parts. I would trade that for having the number of elements
in the table, but it's just my opinion.

>> * "array length": the number of continuous integer keys starting from 1
>
> this suffers from the same O(n) problem as the "number of the highest
> numeric key".

D'oh, you're right, sorry about the noise (but I was advocating the
first definition, anyway) -- now you're officially entitled to hit me
with a stick as well. :)

-- Hisham