lua-users home
lua-l archive

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


On Thu, Jul 31, 2014 at 3:08 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> I read through the definition of "table.copy" and I don't grasp it. I
>> assume that the updated reference isn't complete. The definition is:
>>
>> table.copy (a1, f, e, [a2,] t)
>>
>> Reading the text, I think that:
>>
>> a1 is source
>> a2 is target. if a2 is missing then a1 is used.
>> t is the target index's start, such that the first index to be copied.
>> (shouldn't this default to 1 or f?)
>> f is "first"?
>> e is "end"?
>
> Yes.
>
>
>> so:
>>
>> table.copy(source, 1, 10, destination, 5)
>>
>> would copy index 1 ... 10 into the table 'destination', starting at index 5
>>
>> Is that correct?
>
> Yes.
>
>
>> Is this interface mimicking some pattern that I haven't seen before?
>
> Not that we know. Do you suggest something else?
>
>
>> I didn't find this easy to read or understand.
>
> :(  (Well, in the end you did understand it all, correctly :)
>
> -- Roberto
>

The implementation I often see is:
table.clone = function(table)
  local res = {}
  for k,v in pairs(table) do
    res[k]=v
  end
  return res
end

i.e. given a table, return a shallow copy of it. While this isn't
quite the same operation, I do think table.copy should return a2 (or
a1, if a2 isn't provided) for simplicity's sake, and the defaults for
f, e, t ought to be 1, #a1, 1. Then you would be able to write:
myCopy = table.copy(myTable, {}) --copy into an empty table and return it

though, now we're getting kind of ugly with optional parameters in
strange places...

As an aside, I've been really looking forward to the bitwise
operators, UTF-8 library, and a few of these other changes. :D
-- 
Sent from my Game Boy.