lua-users home
lua-l archive

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




On 31/05/16 02:00 PM, Justin Donaldson wrote:


On Tue, May 31, 2016 at 10:19 AM, Javier Guerra Giraldez <javier@guerrag.com <mailto:javier@guerrag.com>> wrote:

    On 31 May 2016 at 05:42, Justin Donaldson <jdonaldson@gmail.com
    <mailto:jdonaldson@gmail.com>> wrote:
    > it needs to track its length in order to accommodate nil values


    The usual approach to store nil values in a table is to use your own
    'pseudo nil' value.


Consider creating an array [1,2,3], and then setting the index 100 to 4. The indexes 4-99 will still be nil. The length of that array reported by maxn/# would still be indeterminate. How do I resolve that?



With a little help from C, it's possible to create a very efficient coroutine-based nil-safe array. Just need a function exchange(index, value, ...) -> ... (that's a varargs and a varret, respectively), which changes the value in `...` at index `index` with `value` and returns the result. Everything else can be done from Lua. It works better as a stack, however.

Length operation is select('#', ...), push is `val, ...` and pop is `select(2, ...)`. See http://sprunge.us/CGCT

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.