lua-users home
lua-l archive

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


On Sun, Aug 24, 2003 at 09:13:47AM +0200, Tobias K?s wrote:
> >From the manual / basic library:
> 
> unpack (list)
> Returns all elements from the given list. This function is equivalent to
>   return list[1], list[2], ..., list[n]
> 
> except that the above code can be written only for a fixed n. The number n
> is the size of the list, as defined for the table.getn function.
> 
> 
> It looks like you cannot write it yourself with a variable size of the
> table.

Actually I think you can (untested)

function my_unpack(t)
  if getn(t) == 0 return nil
  else if getn(t) == 1 then return t[1]
  else
    newt = {}
    for i=2,getn(t) do newt[i=1] = t[i] end
    return t[1],my_unpack(newt)
  end
end


...I'm not suggesting this is better than unpack, I'm just pointing
out that I think the function *can* be written, because return
automatically 'unpacks' its last argument.

Jules