lua-users home
lua-l archive

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





On Thu, Jul 31, 2014 at 2: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

 

All of the information efficiently presented and present...

Original:
`Copies elements from table a1 to table a2. This function performs the equivalent to the following multiple assignment: a2[t],··· = a1[f],···,a1[e]. The default for a2 is a1. The destination range can overlap with the source range. Index f must be positive.`

The second sentence is a bit difficult for me to parse. It may have been clearer to me with something like:

"`f` and `e` define the beginning and end indexes to copy, while `t` defines the starting target index for a2."

I'm not sure that a different syntax is needed, but having to define the start, end and target index seems surprisingly verbose, when defaults would be assumed to be common (1, #a1, 1)

If that's the case:

table.copy(a1,[a2],[f], [e],[t])

But my suggestion is clouded by a lack of context. What are the envisioned use cases for it / where does this come up? Is it faster than a for loop?

I also agree that "table.copy" seems to suggest "any key value". Obviously its definition clears that up. "table.icopy" seems to fit "pairs" and "ipairs", but I don't mean to nitpick. 

It was mentioned that "table.clear" (or similar) was likely. Was it decided that it would not be useful?

-Andrew