[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: table.maxn...
- From: Andrew Starks <andrew.starks@...>
- Date: Sun, 31 May 2015 10:08:48 -0500
On Saturday, May 30, 2015, Brigham Toskin <brighamtoskin@gmail.com> wrote:
This is kind of an interesting point, actually. Does anyone know the rationale for not just automatically tracking the number of entries in a table? Similar data structures in many other languages do this. I know that's not in an of itself a reason to do anything, but it does seem useful. If you know you're dealing with a sequence a sequence, then #t effectively becomes constant instead of logarithmic. And if you don't know it's a sequence (or you know it's not) then you have a convenient counting method that doesn't involve an iterator.
Is it just the (perceived or actual) runtime overhead?
I believe that it would be complicated to write a generally efficient maxn method.
Sometimes tracking `n` is good? I do that when packing arguments into a table because some arguments are nil.
I have a keyframe object that needs to be ordered by a the time value, which is not a sequence:
`{{0, [value]}, {100, [value]}}`
This works better for me. When I am treating this as a timeline/sequence, `#` evaluates to 100. As a keyframe list, it's 2.
A general `maxn` may have guided me to a different solution, but I don't think it would have been better. It's presence would have somewhat obliged me to use it, if I wanted it to be more accessible to Lua programmers. It's absence caused me to think about the problem more deeply and I think the result is better.
- Andrew
I apologize for adding to the volume of "Lua / array (part) / sequence / length" material. I thought it worth pointing out that features can have the unintended... soft consequences.