  lua-l archive

• Subject: Re: New function table.edge (was Re: Lua 5.2 Length Operator and tables (bug?))
• From: Dirk Laurie <dirk.laurie@...>
• Date: Fri, 27 Apr 2012 16:11:31 +0200

```There has been no discussion of the proposal below, which means that
it is either above or below criticism.

>
> ~~~~
> * table.edge(tbl[,k])
>
> Returns the first at most k edges in tbl, where an edge is a
> non-negative integer n such that tbl[n] is not nil but tbl[n+1] is
> nil, except that 0 is an edge when tbl is nil no matter what tbl
> is.  edge(tbl,-k) returns the last at most k edges.  In particular,
> edge(tbl,-1) returns the largest positive integer index in the table,
> or 0 if the table is empty.
>
> edge(tbl,0) returns one arbitrarily chosen edge.  Which edge this
> will be is undefined except when there is only one edge, i.e. the
> table is empty or a proper sequence, in which case edge(tbl) returns
> the number of elements in the sequence.
>
> The default value of k is 0.
> ~~~~
>
> I.e.
>
>     t={nil,2,3,nil,5,6,7,8,nil,10,=30}
>     table.edge(t,1)   --> 0
>     table.edge(t,100) --> 0,3,8,10,30
>     table.edge(t,-1)  --> 30
>     table.edge(t,0)   --> 10 -- for Lua 5.2 on my laptop
>
> During the discussion of the length operator, one could simply say that
> by default, #tbl returns table.edge(tbl), and that only in the case of
> sequences does this correspond to the intuitive notion of length.
>

```

• References: