lua-users home
lua-l archive

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


>>> RLake@oxfam.org.uk 01/14/03 12:23 PM >>>

> ... Lots of stuff cut out

> 2) hidden keys. A hidden key is one which does not show up in an
iteration
> sequence, and would be useful to hide keys like the memoised function
in a
> memoising functable, etc. This can be implemented with the generalised
for
> statement, but I really wish that I could define the default iterator
for a
> table through a __next metamethod.

You can (sort of) do this already at least in your To_Functable
implementation, by using another table to store the function. This
leaves the "funced" table keys clean, just containing the appropriate
data. Thus:

 do
    local funcs = {}
    local marker = {}  -- Note marker is now just the metatable
    function marker.__index(t,k) return funcs[t](k) end
    function marker.__call(t,v) return t[v] end
    function To_Functable(t,fn)
      funcs[t] = fn
      setmetatable(t, marker)
      return t
    end
  end

Would by the adjusted version of your To_Functable function. (Warning:
untested code). 

The problem here is that the func table will accumulate functions and
tables, not allowing them to be garbage collected. The answer is, of
course, to make the "func" table a weak table.

 Modifying this code to make "func" a weak table is a one liner, but
it's a one-liner I can't easily look up right now (my main machine died
a few days ago and I'm still recovering). Maybe someone could post the
mod?

This doesn't solve the general problem, but in this case it does keep
from polluting the table so you can use normal table iterators.

  - Tom Wrensch

> I freely accept that these are features which might only be of
interest to
> me.

> I hope someone out there found this interesting. :)

R.